PHP Ternary operator Knowledge Summary
Ternary operator syntax: condition? Result 1: Result 2 Description: The position in front of the question mark is judged by the condition, and if the condition is satisfied when the result is 1, the result 2 is not satisfied. Let's take a detailed discussion below.
Today in the change paper online encountered a statement can not understand:
?
1$if_summary = $row [' if_summary ']==2? ' Yes ': ' No ';
Later found in Baidu is the ternary operator of PHP
The meaning of this sentence is equal to
?
1
2
3
4
5if ($row [' if_summary ']==2) {
$if _summary= "yes";
}else{
$if _summary= "No";
}
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:
?
1
2
3
4
5
6if (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:
?
1$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 very obvious to us that multiple execution statements can be used with a string operation symbol ("."). Together, each execution statement is surrounded by small angle brackets to indicate that it is a separate 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:
?
1
2$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:
?
1$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 ';
The above mentioned is the whole content of this article, I hope you can like.
http://www.bkjia.com/PHPjc/1025904.html www.bkjia.com true http://www.bkjia.com/PHPjc/1025904.html techarticle PHP ternary operator knowledge Summary ternary operator syntax: condition? result 1: Result 2 Description: The position in front of the question mark is the judging condition, if the condition is satisfied when the result is 1, the result is not satisfied ...