Wedding Songs for Weddings The binding introduction of PHP ternary operators

Source: Internet
Author: User
Let's look at a ternary operation:

Copy the Code code as follows:


$a =1; $b =2; $c =3; $d = 4;
echo $a < $b? ' XX ': $a < $c? ' YY ': $a < $d? ' ZZ ': ' OO ';
?>


Generally in other languages (such as C or Java) Rules, the above code of the Operation logic is:

Copy the Code code as follows:


$a < $b = True = ' xx ' ==> end


Then the final result is ' xx ', and the subsequent operations will be ignored.
Amazingly, however, the result of PHP's above code is ' zz ' ... I rub, what situation, this does not hang dad ...
The usual, had to ask Google sauce, the result was told that PHP ternary operation is to the left combination of ... So suddenly enlightened.
I add two parentheses to the code above:

Copy the Code code as follows:


$a =1; $b =2; $c =3; $d = 4;
Echo ($a < $b? ' XX ': $a < $c)? ' YY ': $a < $d)? ' ZZ ': ' OO ';
?>


At a glance, this is the operational logic of PHP:

Copy the Code code as follows:


$a < $b + true + ' xx ' + true = ' yy ' = ' true ' + ' zz ' = end


This involves the process of two types of conversions, that is, ' xx ' = = True and ' xx ' = True.
I do not know whether such a process is egg pain, it is really difficult to understand ...
Finally, go back to the code above and turn it into the right combination like C:

Copy the Code code as follows:


$a =1; $b =2; $c =3; $d = 4;
echo $a < $b? ' XX ':($a < $c? ' YY ':($a < $d? ' ZZ ': ' oo '));
The parentheses change position, and the PHP brackets are not
?>

The above describes the wedding song suitable for the marriage of the song PHP ternary operator of the introduction, including the wedding song suitable for the song aspect of the content, I hope the PHP tutorial interested in a friend helpful.

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