tags:

views:

172

answers:

1

Are elseif and else if completely synonymous?

Does Zend have an accepted "standard" on which one to use?

I personally greatly dislike seeing elseif in the code. Just use else if like every other language, right? But I just need to know if they're synonymous and the PHP manual isn't the easiest to search.

+17  A: 

From the PHP manual:

In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

Essentially, they will behave the same, but else if is technically equivalent to a nested structure like so:

if (first_condition)
{

}
else
{
  if (second_condition)
  {

  }
}

The manual also notes:

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.

Which means that in the normal control structure form (ie. using braces):

if (first_condition)
{

}
elseif (second_condition)
{

}

either elseif or else if can be used. However, if you use the alternate syntax, you must use elseif:

if (first_condition):
  // ...
elseif (second_condition):
  // ...
endif;
Daniel Vandersluis
Sir, I need an example, please.
hopeseekr
Good job with the example snippets. If anything, the alternate syntax is all that's necessary to prove that PHP simply treats `else if` as `else { if {`.
BoltClock