Php ternary operator knowledge

Source: Internet
Author: User

Php ternary operator knowledge

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:

?

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

?

1

2

3

4

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

?

1

2

3

4

5

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

?

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

?

1

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

?

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

Related Article

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.