Home  >  Article  >  Backend Development  >  这个if和这个三元是等价的吗

这个if和这个三元是等价的吗

WBOY
WBOYOriginal
2016-06-23 13:39:251021browse

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

            if($no1%3==1){
                $num1=1;
             }elseif($no1%3==2){
                $num1=2;
             }elseif($no1%3==0){
                $num1=3;
             }
试了一下,if的显示,而三元的不显示,不知哪里错了


回复讨论(解决方案)

$num=3:false; 这个地方前面是赋值,后面直接是个false了。。

看着阵势,楼主又是那个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));

你的三元和if是不等价的,仔细看看你的三元只会得出两种结果,$num=3或false

上面答案不是给楼主的,给只会用 if , switch 的盆友们提供另外一种思路,就是用hashtable,分支多时它的效率是最高的

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn