[ACCEPTED]-What does ? ... : ... do?-ternary-operator

Accepted answer
Score: 45

This is a ternary operator:

The expression (expr1) ? (expr2) : (expr3) evaluates to 2 expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to 1 FALSE.

Score: 18

That last part is known as the conditional operator. Basically 9 it is a condensed if/else statement.

It works like 8 this:

$items =
    // if this expression is true
    // then "$_POST['items']" is assigned to $items
    ? $_POST['items'] 
    // else "array()" is assigned
    : array();

Also here is some pseudo-code that 7 may be simpler:

$items = (condition) ? value_if_condition_true : value_if_condition_false;

Edit: Here is a quick, pedantic 6 side-note: The PHP documentation calls this 5 operator a ternary operator. While the conditional operator 4 is technically a ternary operator (that 3 is, an operator with 3 operands) it is a 2 misnomer (and rather presumptive) to call 1 it the ternary operator.

Score: 2

It is the same as:

if (isset($_POST['items']){
    $items = $_POST['items'];
} else {
    $items = array();


Score: 2

Look at Paolo's answer to understand the 13 ternary operator.

To do what you are looking 12 at doing you might want to use a session 11 variable.

At the top of your page put this 10 (because you can't output anything to the 9 page before you start a session. I.E. NO 8 ECHO STATEMENTS)


Then when a user submits 7 your form, save the result in this server 6 variable. If this is the first time the 5 user submitted the form, just save it directly, otherwise 4 cycle through and add any value that is 3 not empty. See if this is what you are looking 2 for:

HTML CODE (testform.html):

    <form name="someForm" action="process.php" method="POST"> 
        <input name="items[]" type="text">
        <input name="items[]" type="text">
        <input name="items[]" type="text">
        <input type="submit">

Processing 1 code (process.php):


if(!$_SESSION['items']) {
    // If this is the first time the user submitted the form,
    // set what they put in to the master list which is $_SESSION['items'].
    $_SESSION['items'] = $_POST['items'];
else {
    // If the user has submitted items before...
    // Then we want to replace any fields they changed with the changed value
    // and leave the blank ones with what they previously gave us.
    foreach ($_POST['items'] as $key => $value) {
        if ($value != '') { // So long as the field is not blank
            $_SESSION['items'][$key] = $value;

// Displaying the array.
foreach ($_SESSION['items'] as $k => $v) {
    echo $v,'<br>';
Score: 1

yup... it is ternary operator

a simple and 8 clear explanation provided here, in which the 7 author said it is like answering : “Well, is it true?”

the colon separates two 6 possible values (or). the first value will 5 be chosen if the test expression is true. the 4 second (behind the colon) will be chosen 3 if the first answers is false.

ternary operator 2 very helpfull in creating variable in php 1 7.x, free of notice warning. For example"

$mod = isset($_REQUEST['mod']) ? $_REQUEST['mod'] : "";
Score: 0

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

Score: 0

It is a ternary operator that essentially 3 says if the items key is in the $_POST then 2 set $items to equal the value of $_POST['items'] else 1 set it to a null array.

Score: 0

I figured it's also worth noting that ?: is 6 a separate operator, where:

$one = $two ?: $three;
$one = two() ?: three();

is shorthand 5 for:

$one = $two ? $two : $three;
$one = two() ? two() : three();

Aside from typing less, the runtime 4 advantage is that, if using a function like 3 two(), the function would only be evaluated once 2 using the shorthand form, but possibly twice 1 using the long form.

More Related questions