What does it mean? ...: ... do?

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

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

What does this code combination do for sure?

I use it to take a bunch of values ​​from html text fields and store them in an array of sessions. But the problem is that if I try to resend the data to text fields, the new array session will overwrite the old array of sessions completely empty and all.

I only want to overwrite the places in the array that already have values. If the user decides to fill in only a few text fields, I do not want the previous data in the session array to be overwritten with empty space (from empty text fields).

I think this code is a problem, but I'm not sure how this works. Enlighten me, please.

+16
syntax ternary-operator php
May 20 '09 at 18:04
source share
8 answers

This is a ternary operator :

Expression (expr1) ? (expr2) : (expr3) (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE and expr3 if expr1 evaluates to FALSE .

+38
May 20 '09 at 18:05
source share

This last part is called the conditional statement. This is basically a compressed if/else .

It works as follows:

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

There is also some pseudo code here, which could be simpler:

 $items = (condition) ? value_if_condition_true : value_if_condition_false; 



Edit: Here is a short, pedantic note. The PHP documentation calls this statement the ternary statement. While the conditional operator is technically a ternary operator (i.e., an operator with 3 operands), it is incorrect (and rather implied) to call it a termar operator .

+17
May 20 '09 at 18:05
source share

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

To do what you are looking for, you can use a session variable.

Put this at the top of the page (because you cannot display anything on the page before starting a session. IE NO ECHO STATEMENTS)

 session_start(); 

Then, when the user submits your form, save the result in this server variable. If this is the first time a user has submitted a form, just save it directly, otherwise run a loop and add any value that is not empty. See what exactly you are looking for:

HTML CODE (testform.html):

 <html> <body> <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"> </form> </body> </html> 

Code Processing (process.php):

 <?php session_start(); 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>'; } ?> 
+2
May 21 '09 at 17:31
source share

This is the same as:

 if (isset($_POST['items']){ $items = $_POST['items']; } else { $items = array(); } 
+1
May 20 '09 at 18:09
source share

Basically, if $ _POST ['items'] exists, then $ items is set to it, otherwise it gets the value into an empty array.

0
May 20 '09 at 18:06
source share

This is a ternary operator, which essentially says that if the item key is in $ _POST, then set $ items to $ _POST ['items'], otherwise set it to a zero array.

0
May 20, '09 at 18:08
source share

I thought it was also worth noting that ?: Is a separate statement, where:

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

is an abbreviation for:

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

Apart from less input, the advantage at run time is that if you use a function like two() , the function will be evaluated only once using the short form, but possibly twice using the long form.

0
Jun 06 '16 at 18:11
source share

yup ... is a ternary operator

a simple and understandable explanation is provided here , in which the author said that this is similar to the answer: "Well, is this true?

the colon separates the two possible values ​​(or). the first value will be selected if the test expression is true. the second (after the colon) will be selected if the first answers are false.

the ternary operator is very useful when creating a variable in php 7.x, without warning. For example,

 $mod = isset($_REQUEST['mod']) ? $_REQUEST['mod'] : ""; 
0
Sep 16 '17 at 6:20
source share



All Articles