Is this if equivalent to this ternary element?

Source: Internet
Author: User
Is this if equivalent to this ternary ($ no % 3 = 1 )? $ Num = 1 :( $ no % 3 = 2 ))? $ Num = 2 :( $ no % 3 = 0 ))? $ Num = 3: false;
And
If ($ no1 % 3 = 1 ){
$ Num1 = 1;
} Elseif ($ no1 % 3 = 2 ){
$ Num1 = 2;
} Elseif ($ no1 % 3 = 0 ){
$ Num1 = 3;
}
I tried it. if is displayed, but if is not displayed, I don't know where it is wrong.


Reply to discussion (solution)

$ Num = 3: false; in this case, the value is first assigned, and the value is directly false ..

Looking at the trend, the poster is sheep_app3.

$ Map = array (3, 1, 2); $ num1 = $ map [$ no % 3];

($ No % 3 = 1 )? $ Num = 1 :( ($ no % 3 = 2 )? $ Num = 2 :( ($ no % 3 = 0 )? $ Num = 3: false ));

Your ternary and if are not equivalent. take a closer look at your ternary and only get two results, $ num = 3 or false.

The above answer is not for the landlord, but for those who only use if and switch, another idea is to use hashtable, which has the highest efficiency when many branches exist.

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.