PHP ternary operator

Source: Internet
Author: User

Recently in the project with a nested ternary operation, take it for granted that no problem, no test on the line (the problem is not good), the results were humiliated by colleagues.

Conditions? True: false, test condition first, if True then execute? After the statement, if False, executes: after the statement.

True?1:-1, the result of the expression is the result of a 1,false?1:-1 expression of-1.

This is not difficult to understand, and I was wrong in nesting when I do not know its binding, is left to combine rather than take for granted right combination.

1 = = $a? 1:2== $a? 2:3

I want to say that if $ A is 1 it returns 1, 2 returns 2, and the other returns 3. The result of the expression above will never be 1;

The above expression is with (1 = = $a? 1:2== $a)? 2:3 equivalent.

And what I really need is 1 = = $a? 1: (2== $a? 2:3)

It is a good habit, by the way, to add parentheses.

PHP ternary operator

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.