只需要了解用于设置POSTed var [duplicate]的PHP语法

This question already has an answer here:

So I'm using the following php code to set variables that are received from a POST method, but I'm interested in how it works.

$var1 = isset($_REQUEST['var1']) ? $_REQUEST['var1'] : 'default';

I understand what it does, but I don't understand the syntax.

Thanks for the help :)

</div>

This is a short hand IF statement and from that you are assigning a value to $var1

The syntax is :

$var = (CONDITION) ? (VALUE IF TRUE) : (VALUE IF FALSE);

? is just a short and optimised notation of doing this:

if (isset($_REQUEST["var1"])) // If the element "var1" exists in the $_REQUEST array
   $var1 = $_REQUEST["var1"]; // take the value of it
else
   $var1 = "default"; // if it doesn't exist, use a default value

Note that you might want to use the $_POST array instead of the $_REQUEST array.

It's the synatx of the ternary operator. It's shorthand for if/else. Please read PHP Manaul

This is a 'ternary operator', what it says is:-

If var1 is set as a post variable then set var1 to that value, otherwise setvar1 to be the string 'default'. Using traditional syntax it would be:-

if (isset($_REQUEST('var1')) { $var1 = $_REQUEST('var1'); } else { $var1 = 'default'; }

You probably mean ternary operator

Syntax it's same like

if(isset($_REQUEST('var1') ) {
    $var1 = ? $_REQUEST('var1')
}else {
    $var1 =: 'default';
}

its a short way of doing an if. if you are expecting a post variable its must better to use _POST rather than request.

the "?" says if the isset($_REQUEST) is true, then do the everything between the ? and : otherwise do everything between the : and the ;