21

I was wondering the differnce between elseif and else if.

I was reading the manual which says if using the braces {} they are treated the same but only elseif works when not using the braces.

Also another contributer said:

Note that } elseif() { is somewhat faster than } else if() {

and he backed it up with a benchmark test.

To me it seems like elseif is the true way of saying it and saying:

else if() {...}

is really the equivalent of:

else { if() {...} }

which might explain why its marginally slower.

I am used to using else if because thats how I do it in other languages as well. I don't really think it matters which way you type it (unless you are not using braces.. but I think its always good to use braces anyways) I was just curious about the underlying details.

Am I correct?

JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
  • 3
    See the answer to this question: http://stackoverflow.com/questions/3662412/are-elseif-and-else-if-completely-synonymous Doesn't cover performance but may *suggest* that PHP treats `else if` as `else { if }`. – BoltClock Jan 11 '11 at 19:12
  • This question is certainly a dupe of my original one. – Theodore R. Smith Jan 16 '14 at 15:17
  • 2
    Possible duplicate of [Are "elseif" and "else if" completely synonymous?](http://stackoverflow.com/questions/3662412/are-elseif-and-else-if-completely-synonymous) – Prifulnath May 16 '17 at 12:11
  • This references speed of operation while the other didn't – Barry Chapman Sep 25 '18 at 03:33

4 Answers4

9

In Manual I read out(See Note):

Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.

Example:

<?php

/* Incorrect Method: */
if($a > $b):
    echo $a." is greater than ".$b;
else if($a == $b): // Will not compile.
    echo "The above line causes a parse error.";
endif;


/* Correct Method: */
if($a > $b):
    echo $a." is greater than ".$b;
elseif($a == $b): // Note the combination of the words.
    echo $a." equals ".$b;
else:
    echo $a." is neither greater than or equal to ".$b;
endif;

?>
Chirag Parekh
  • 498
  • 6
  • 13
8

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • And, if i'm not wrong, PHP shouldn't care about spaces at all. – Shoe Jan 11 '11 at 19:30
  • @Charlie Pigarelli: Whitespace isn't an issue, it's the fact that Python and PHP's alternative syntax choke on seeing an `if` instead of a colon after `else`. – BoltClock Jan 11 '11 at 19:31
1

The "speed difference" in that comment is not statistically significant. Aside from else if: not parsing, there's no effective difference.

Amber
  • 507,862
  • 82
  • 626
  • 550
-3

Regarding the efficiency, and based on what I have been studying for a long time. There is no difference.

dlock
  • 9,447
  • 9
  • 47
  • 67