What does ? … : … do? [duplicate]

2019-01-04 11:27发布

This question already has an answer here:

$items = (isset($_POST['items'])) ? $_POST['items'] : array();

I don't understand the last snippet of this code "? $_POST['items'] : array();"

What does that combination of code do exactly?

I use it to take in a bunch of values from html text boxes and store it into a session array. But the problem is, if I attempt to resubmit the data in text boxes the new array session overwrites the old session array completely blank spaces and all.

I only want to overwrite places in the array that already have values. If the user decides to fill out only a few text boxes I don't want the previous session array data to be overwritten by blank spaces (from the blank text boxes).

I'm thinking the above code is the problem, but I'm not sure how it works. Enlighten me please.

8条回答
Rolldiameter
2楼-- · 2019-01-04 12:07

It is the same as:

if (isset($_POST['items']){
    $items = $_POST['items'];
} else {
    $items = array();
}
查看更多
Fickle 薄情
3楼-- · 2019-01-04 12:07

Basically if $_POST['items'] exists then $items gets set to it otherwise it gets set to an empty array.

查看更多
登录 后发表回答