Three element operators in php

Source: Internet
Author: User
Tags php tutorial readable

Three element operators in the php Tutorial


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 refined and the execution efficiency is high. The proper use of ternary operators in PHP programs makes scripts more concise and efficient. The code syntax 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;
}
Obviously, it is not an exaggeration to use the ternary operators. 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 readable and may cause problems in code maintenance in the future. However, they are more readable than "if .... compared with process statements such as else, it is very concise, which is its benefit.

In use, you can also ignore 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 following syntax is correct, and 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 to replace the echo statement ----------- In The php4 environment. If you use the echo statement to print the content, php reports an error

 

The ternary operator can also be used to assign values:


$ A = 2;
$ B = 1;
$ Str = ($ a> $ B): 'yes': 'no ';
Echo $ str;

 

Echo
$ A = 1? 'One ':
$ A = 2? 'Two ':
$ A = 3? 'Three ':
$ A = 4? 'Foura ': 'Other ';
Echo "n ";

The output result is:

 

 

Result: four

I cannot understand it at first. According to my understanding, the logic is as follows:
Echo ($ a = 1? 'One ':
($ A = 2? 'Two ':
($ A = 3? 'Three ':
($ A = 4? 'Four': 'Other '))));
Output: two

Later, with the help of kevinG (qq: 48474), I read the php manual and finally understood that the interpretation of the php trielement was from left to right,
<! --? Php
// At first glance, the output below is 'true'
Echo (true? 'True': false? 'T': 'F ');

// However, the actual output of the preceding statement is 'T', because the ternary operators are calculated from left to right.

// The following statements are equivalent to the preceding statements, but they are clearer.
Echo (true? 'True': 'false ')? 'T': 'F ');

So the above can be explained as follows:
Echo (
($ A = 1? 'One ':
$ A = 2 )? 'Two ':
$ A = 3 )? 'Three ':
$ A = 4 )? 'Four': 'Other ');

In fact, this involves type conversion:
$ A = 1 => false => $ a = 2? True => 'two' = true => 'Three '= true => 'four'

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.