Ternary operator Syntax: condition? Result 1: result 2: the position before the question mark is the condition for judgment. If the condition is met, result 1 is displayed. If the condition is not met, result 2 is displayed. Next we will discuss in detail. Today, when I changed my paper online, I encountered a statement that I couldn't understand:
$ If_summary = $ row ['if _ SUMMARY '] = 2? 'Yes': 'no ';
Later, Baidu discovered that it was a PHP Trielement operator.
The meaning of this sentence is equal
If ($ row ['if _ SUMMARY '] = 2) {$ if_summary = "yes";} else {$ if_summary = "no ";}
The functions of the ternary operator are the same as those of the "if... else" flow statement. It is written in one line, and the code is very refined and the execution efficiency is higher.
The proper use of ternary operators in PHP programs can make scripts more concise and efficient.
The code format is as follows: (expr1 )? (Expr2): (expr3 );
Explanation: if the condition "expr1" is true, execute the statement "expr2"; otherwise, execute "expr3 ".
To implement the same function, if you use conditional flow statements, you need to write multiple lines of code:
if(expr1) {expr2;} else {expr3;}
It can be seen that the three-element operators mentioned above are not exaggerated. However, in most cases, we only use the ternary operator when the code is relatively simple, that is, when the execution statement is only a single sentence. For example:
$ A> $ B? Print "a greater than B": print "a less than B ";
In fact, the ternary operators can be extended and used. When the set conditions are true or not true, more than one statement can be executed, in the following format:
(Expr1 )? (Expr2). (expr3): (expr4). (expr5 );
We can see that multiple execution statements can use the string operator number (". ), and each execution statement is enclosed by small angle brackets to indicate that it is an independent and complete execution statement. In this way, its function is closer to the "if... else" flow statement.
The ternary operators can also be nested. For example, when a is greater than B, if a is less than c, then x = c-a; otherwise, x = a-c; otherwise, a is less than B. 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 ternary operators used for nesting are not very readable, and the maintenance of code in the future is very likely to have problems, but compared with "if... in the above circumstances, it is indeed too concise, it is attractive.
For those who like to be lazy and pursue concise code, replacing the if flow statement with a ternary operator should be an excellent choice. Even if you do not need to consider any element other than the conditional sentence in the "ternary", the use of the ternary operator is still more concise than the if statement. The syntax of the following statements is correct. They ignore the second or third "RMB" in the form of small quotation marks ":
$a>$b ? print "Yes" : "";$a>$b ? '': print 'No';
Note: We recommend that you use the print statement instead of the echo statement when using the ternary operator.
Note the following statements:
$str = $_GET['abc'] ? 'wangjinbo' : 'wjb';
This cannot be understood as: When $ str is equal to $ _ GET ['abc'], the value is assigned to 'wangjinbo'; otherwise, the value is assigned to 'wjb '; because 1: to determine equality, use =; because the syntax of the original binary: ternary operator is as shown above: (expr1 )? (Expr2): (expr3). Obviously, the preceding binary, ternary 'wangjinbo' or 'wjb 'cannot constitute a meaningful expression separately;
Correct understanding: When $ _ GET ['abc'] is null (that is, whether, in PHP, '', null, 0, undifine, are equivalent to boolean values false, assign $ str to 'wangjinbo'; otherwise, assign 'wjb ';
The above is all the content of this article. I hope you will like it.