The function of the ternary operator is consistent with the "if...else" process statement, which is written in a single line, the code is very concise, and the execution is more efficient.
Using the ternary operator properly in a PHP program can make the script more concise and efficient.
The code format is as follows: (EXPR1)? (EXPR2): (EXPR3);
Explanation: If the condition "Expr1" is true, the statement "EXPR2" is executed, otherwise "EXPR3" is executed.
To achieve the same functionality, if you use conditional process statements, you need to write multiple lines of code:
if (EXPR1) {
EXPR2;
} else {
EXPR3;
}
It can be seen that the good of the ternary operator is not exaggerated. However, in most cases we only use the ternary operator when the code is simpler, that is, when the execution statement is only a single sentence. Such as:
$a > $b? Print "A is greater than B": print "A is less than B";
In fact, the ternary operator can be extended to use, when the conditions set or not, the execution of the statement can be more than one sentence, sample the following format:
(EXPR1)? (EXPR2). (EXPR3): (EXPR4). (EXPR5);
It is clear to us that multiple execution statements can be used with a string operation symbol ("."). ), each execution statement is surrounded by small angle brackets to indicate that it is an independent and complete execution statement. This expands its functionality closer to the "if...else" process statement.
The ternary operator can also be used in a nested set. For example, a greater than B is established: If A is less than C, then x=c-a otherwise x=a-c; otherwise a less than B is established: If B is less than C, then x=c-b otherwise x=b-c:
$a > $b? $x = ($a < $c? $c-$a: $a-$c): $x = ($b < $c? $c-$b: $b-$c);
The use of the ternary operator is not very readable, the future maintenance of the code is likely to be problematic, but compared to the "If...else" and other process statements, in the above situation, it is too concise, this is its tempting.
For those who prefer to be lazy and pursue code brevity, using ternary operators instead of if process statements should be an excellent choice. Even if you don't have to consider any of the "meta" elements in the "ternary" clause, using the ternary operator is still more concise than the IF statement. The syntax for the following statements is correct, and they omit the second or third "meta" in pee quotes:
$a > $b? Print "Yes": "";
$a > $b? ": print ' No ';
It should be noted that the use of the print statement instead of the Echo statement is recommended when using the ternary operator.
Note the understanding of the following sequence of statements:
$STR = $_get[' abc ')? ' Wangjinbo ': ' WJB ';
This cannot be understood as: when $str equals $_get[' abc ', the assignment is ' Wangjinbo ' otherwise assigned to ' WJB '; because one: judgment equality should be used = =; Because the original two: The syntax of the ternary operator is as shown above: (EXPR1)? (EXPR2): (EXPR3), apparently above the two yuan, ternary ' Wangjinbo ' or ' WJB ' cannot constitute a meaningful expression alone;
The correct understanding is: When $_get[' abc ' is empty (also whether, PHP ", Null,0,undifine, are equivalent Boolean false), the $STR is assigned to ' Wangjinbo ', otherwise assigned to ' WJB ';
This article is from the "12145704" blog, please be sure to keep this source http://12155704.blog.51cto.com/12145704/1868141
PHP ternary operator