underlying difference between elseif vs else if

后端 未结 4 750
我寻月下人不归
我寻月下人不归 2020-12-17 10:39

I was wondering the differnce between elseif and else if.

I was reading the manual which says if using the braces {} they are

4条回答
  •  佛祖请我去吃肉
    2020-12-17 10:51

    In addition to my comment, from what I can see in the PHP source, the only speed boost is that PHP's parser has a special token for elseif, unsurprisingly known as T_ELSEIF. But I'm sure the speed boost is nothing more than a side effect of allowing elseif in the alternative syntax:

    if (cond1):
        // Do something
    elseif (cond2):
        // Do something else
    else:
        // Do something else
    endif;
    

    Similar to Python, which uses elif, else if with a space wouldn't be allowed without having to unnecessarily complicate the parser's implementation code (i.e. to act differently between seeing a colon and the if keyword). So a colon is required directly after else and if respectively, and that's how the language rule is stipulated.

    Ergo I'm pretty sure PHP just treats else if as a conditional block in a singular-statement else.

    If there are any speed differences at all, blame the parser for most of them.

提交回复
热议问题