Let's look at a ternary operation:
Copy the Code code as follows:
$a =1; $b =2; $c =3; $d = 4;
echo $a < $b? ' XX ': $a < $c? ' YY ': $a < $d? ' ZZ ': ' OO ';
?>
Generally in other languages (such as C or Java) Rules, the above code of the Operation logic is:
Copy the Code code as follows:
$a < $b = True = ' xx ' ==> end
Then the final result is ' xx ', and the subsequent operations will be ignored.
Amazingly, however, the result of PHP's above code is ' zz ' ... I rub, what situation, this does not hang dad ...
The usual, had to ask Google sauce, the result was told that PHP ternary operation is to the left combination of ... So suddenly enlightened.
I add two parentheses to the code above:
Copy the Code code as follows:
$a =1; $b =2; $c =3; $d = 4;
Echo ($a < $b? ' XX ': $a < $c)? ' YY ': $a < $d)? ' ZZ ': ' OO ';
?>
At a glance, this is the operational logic of PHP:
Copy the Code code as follows:
$a < $b + true + ' xx ' + true = ' yy ' = ' true ' + ' zz ' = end
This involves the process of two types of conversions, that is, ' xx ' = = True and ' xx ' = True.
I do not know whether such a process is egg pain, it is really difficult to understand ...
Finally, go back to the code above and turn it into the right combination like C:
Copy the Code code as follows:
$a =1; $b =2; $c =3; $d = 4;
echo $a < $b? ' XX ':($a < $c? ' YY ':($a < $d? ' ZZ ': ' oo '));
The parentheses change position, and the PHP brackets are not
?>
The above describes the wedding song suitable for the marriage of the song PHP ternary operator of the introduction, including the wedding song suitable for the song aspect of the content, I hope the PHP tutorial interested in a friend helpful.