以前的时候还没注意到,知道进入公司,看的代码多了点,时常看到elseif的分支,而我自己时常用的是else if,于是就注意到,他们既然都可以用,那么他们的却别在哪儿呢? 通过查询,原来,确实有点小小区别:1.一个能用冒号,另外一个不可以 ? php /* Incorre
以前的时候还没注意到,知道进入公司,看的代码多了点,时常看到elseif的分支,而我自己时常用的是else if,于是就注意到,他们既然都可以用,那么他们的却别在哪儿呢?
通过查询,原来,确实有点小小区别:1.一个能用冒号,另外一个不可以
<span>php </span><span>/*</span><span> Incorrect Method: </span><span>*/</span> <span>if</span>(<span>$a</span> > <span>$b</span>): <span>echo</span> <span>$a</span>." is greater than ".<span>$b</span><span>; </span><span>else</span> <span>if</span>(<span>$a</span> == <span>$b</span>): <span>//</span><span> Will not compile.</span> <span>echo</span> "The above line causes a parse error."<span>; </span><span>endif</span><span>; </span><span>/*</span><span> Correct Method: </span><span>*/</span> <span>if</span>(<span>$a</span> > <span>$b</span>): <span>echo</span> <span>$a</span>." is greater than ".<span>$b</span><span>; </span><span>elseif</span>(<span>$a</span> == <span>$b</span>): <span>//</span><span> Note the combination of the words.</span> <span>echo</span> <span>$a</span>." equals ".<span>$b</span><span>; </span><span>else</span>: <span>echo</span> <span>$a</span>." is neither greater than or equal to ".<span>$b</span><span>; </span><span>endif</span><span>; </span>?>