PHP ternary operator Knowledge Rollup _php instance

Source: Internet
Author: User

Today in the change paper online when I encountered a statement can not understand:

$if _summary = $row [' If_summary ']==2?] Is ': ' No ';

Then after Baidu found that PHP is the ternary operator

The meaning of this sentence is tantamount to

if ($row [' if_summary ']==2) {
$if _summary= "yes";
} else{
$if _summary= "no";
}

The ternary operator's functionality is consistent with the "if...else" process statement, which is written in one line, and the code is very concise and performs more efficiently.

Using the ternary operator appropriately 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 established, the statement "EXPR2" is executed, otherwise "EXPR3" is executed.

To achieve the same function, if you use conditional process statements, you need to write multiple lines of code:

if (expr1) {

expr2;}
else {
expr3;
}

As you can see, the ternary operators mentioned above are 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 simple 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 set of conditions or not set up, execute the statement can be more than one sentence, the following format:

(EXPR1)? (EXPR2). (EXPR3): (EXPR4). (EXPR5);

We see very clearly that multiple execution statements can use the string operator notation ("." , each execution statement is surrounded by small brackets to indicate that it is an independent and complete execution statement. This extends its functionality to a more approximate "if...else" process statement.

The ternary operator can also be used in nested nesting. For example, A is larger 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);

Nested ternary operators are not very readable, and future maintenance of the code is likely to be problematic, but rather than "if...else" such as the process statement, in this case, it is too concise, this is its attractive place.
For those who prefer to be lazy and pursue code simplicity, it should be an excellent choice to replace the IF process statement with a ternary operator. Using the ternary operator is still more concise than if, even if you do not consider any of the "meta" criteria in the "ternary" clause. The syntax for the following statements is correct, and they omit the second or third "dollar" in Pee quotes:

$a > $b? Print "Yes": "";
$a > $b? ': print ' No ';

It should be noted that when using the ternary operator, it is recommended that you use the PRINT statement instead of the Echo statement.

Note the understanding of the following list 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: the equality of judgment should use = =; Because the original two: The syntax of the ternary operator is as follows: (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, in PHP, ', Null,0,undifine, all equivalent Boolean value false), the $STR is assigned to ' Wangjinbo ', otherwise the value is ' WJB ';

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.