PHP ternary operator combination and its use

伊谢尔伦
Release: 2023-03-10 22:48:01
Original
1881 people have browsed it

The function of the ternary operation symbol is consistent with the "if...else" process statement. It is written in one line, and the code is very concise and the execution efficiency is higher.

Appropriate use of the ternary operator in PHP programs can make the script 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 achieve the same function, if you use conditional process statements, you need to write multiple lines of code:

if(expr1) { 
expr2;
} else {
expr3;
}
Copy after login

It can be seen that the goodness of the ternary operator mentioned above is not an exaggeration. 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大于b" : print "a小于b";
Copy after login

In fact, the ternary operator can be extended. When the set condition is true or not, the execution statement can be more than one sentence. Try the following format:

(expr1) ? (expr2).(expr3) : (expr4).(expr5);
Copy after login

We are very obvious As you can see, multiple execution statements can be connected using the string operator ("."). Each execution statement is surrounded by small angle brackets to indicate that it is an independent and complete execution statement. After this expansion, its function is closer to the "if...else" process statement.

At the same time, the ternary operator can also be used nested. For example, when a is greater than b: if a is less than c, then x=c-a otherwise x=a-c; otherwise when 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 readability of the nested ternary operator is not very good, and there may be problems with maintaining the code in the future. However, compared with process statements such as "if...else", in the above In this case, it is indeed too simple, which is its attractiveness.
For those who like to be lazy and pursue code simplicity, using the ternary operator to replace the if process statement should be an excellent choice. Even if there is no need to consider any "element" other than the conditional sentence in the "ternary", using the ternary operator is still more concise than the if statement. The syntax of the following statements is correct, they omit the second or third "element" in a small unquote manner:

$a>$b ? print "Yes" : "";
$a>$b ? &#39;&#39;: print &#39;No&#39;;
Copy after login

It should be noted: when using the ternary operator, it is recommended to use the print statement Replaces the echo statement.

Pay attention to the understanding of the following series of statements:

$str = $_GET[&#39;abc&#39;] ? &#39;wangjinbo&#39; : &#39;wjb&#39;;
Copy after login

This cannot be understood as: when $str is equal to $_GET['abc'], the assignment is 'wangjinbo' otherwise the assignment is 'wjb' ; Reason 1: == should be used to judge equality; Reason 2: The syntax of the ternary operator is as shown above: (expr1) ? (expr2) : (expr3), obviously the above binary, ternary 'wangjinbo' or ' wjb' cannot alone form a meaningful expression;

The correct understanding is: when $_GET['abc'] is empty (that is, whether, '' in PHP, null, 0, undifine, are equivalent to Boolean value false), assign $str to 'wangjinbo', otherwise assign it to 'wjb';

Associativity:

<?php 
$a=1;$b=2;$c=3;$d=4; 
echo $a<$b?&#39;xx&#39;:$a<$c?&#39;yy&#39;:$a<$d?&#39;zz&#39;:&#39;oo&#39;; 
?>
Copy after login

Generally follow According to the rules of other languages ​​(such as C or Java), the operation logic of the above code is:

$a<$b => true => &#39;xx&#39; ==> 结束
Copy after login

Then the final result is 'xx', and subsequent operations will be ignored. PHP The ternary operation turns out to be left associative.

<?php 
$a=1;$b=2;$c=3;$d=4; 
echo (($a<$b?&#39;xx&#39;:$a<$c)?&#39;yy&#39;:$a<$d)?&#39;zz&#39;:&#39;oo&#39;; 
?>
Copy after login

It is clear at a glance, this is the operation logic of PHP:

$a<$b => true => &#39;xx&#39; => true => &#39;yy&#39; => true => &#39;zz&#39; => 结束
Copy after login

This involves two types of conversion processes, namely 'xx' => true and 'xx' = > true.
I don’t know if this process is a pain in the butt, it is indeed difficult to understand...
Finally, go back to the above code again and change it into a right combine like C:

<?php 
$a=1;$b=2;$c=3;$d=4; 
echo $a<$b?&#39;xx&#39;:($a<$c?&#39;yy&#39;:($a<$d?&#39;zz&#39;:&#39;oo&#39;)); 
// 括号换下位置就行了, php里括号省不得 
?>
Copy after login

The above is the detailed content of PHP ternary operator combination and its use. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template