PHP ternary operator details, operator _php Tutorial

Source: Internet
Author: User

PHP ternary operator detailed, operator


Today in the change paper online encountered a statement can not understand:
$if _summary = $row [' if_summary ']==2? ' Yes ': ' No ';
Later found in Baidu is the ternary operator of PHP
The meaning of this sentence is equal to
if ($row [' if_summary ']==2) {
$if _summary= "yes";
}else{
$if _summary= "No";
}


The function of the ternary operator is consistent with the "if...else" process statement, which is written in a single line, the code is very concise, and the execution is more efficient.

Using the ternary operator properly 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 true, the statement "EXPR2" is executed, otherwise "EXPR3" is executed.
To achieve the same functionality, if you use conditional process statements, you need to write multiple lines of code:
if (EXPR1) {

EXPR2;
} else {
EXPR3;
}
It can be seen that the good of the ternary operator is 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 single 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 conditions set or not, the execution of the statement can be more than one sentence, sample the following format:
(EXPR1)? (EXPR2). (EXPR3): (EXPR4). (EXPR5);
It is clear to us that multiple execution statements can be used with a string operation symbol ("."). ), each execution statement is surrounded by small angle brackets to indicate that it is an independent and complete execution statement. This expands its functionality closer to the "if...else" process statement.
The ternary operator can also be used in a nested set. For example, a greater than B is established: If A is less than C, then x=c-a otherwise x=a-c; otherwise a less than B is established: 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 use of the ternary operator is not very readable, the future maintenance of the code is likely to be problematic, but compared to the "If...else" and other process statements, in the above situation, it is too concise, this is its tempting.
For those who prefer to be lazy and pursue code brevity, using ternary operators instead of if process statements should be an excellent choice. Even if you don't have to consider any of the "meta" elements in the "ternary" clause, using the ternary operator is still more concise than the IF statement. The syntax for the following statements is correct, and they omit the second or third "meta" in pee quotes:
$a > $b? Print "Yes": "";
$a > $b? ": print ' No ';
It should be noted that the use of the print statement instead of the Echo statement is recommended when using the ternary operator.

Note the understanding of the following sequence 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: judgment equality should be used = =; Because the original two: The syntax of the ternary operator is as shown above: (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, PHP ", Null,0,undifine, are equivalent Boolean false), the $STR is assigned to ' Wangjinbo ', otherwise assigned to ' WJB ';


Original link: bbs.php100.com/read-htm-tid-24239.html

http://www.bkjia.com/PHPjc/1009710.html www.bkjia.com true http://www.bkjia.com/PHPjc/1009710.html techarticle PHP ternary operator in detail, the operator in detail today in the change of paper online encountered a statement can not understand: $if _summary = $row [' if_summary ']==2? ' Yes ': ' No '; Baidu later found that ...

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