PHP Ternary operator Knowledge Rollup

Source: Internet
Author: User

Ternary operator syntax: condition? Result 1: Result 2 Note: The position before the question mark is the condition of judgment, if the condition is satisfied with the result 1, when the result is not satisfied 2. Let's go into the details below.

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

?

1$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

?

1

2

3

4

5if ($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:

?

1

2

3

4

5

6if (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:

?

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 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 (".") Together, the execution statements are surrounded by small angle 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:

?

1

2$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:

?

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: 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.