Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Few days ago I stopped on some PHP's developers quide (for contributors to particular project) and it stated, that elseif must be strictly used instead of else if -- without giving any reason, why?

Can someone clarify this? I don't see neither much difference nor any argument supporting this.

Info: I believe, there are more languages, than just PHP, that this problem strikes, that's why I asked here, not on Stack Overflow. Feel free to migrate, if necessary.

share|improve this question
This is answered in SO – Srihari May 30 '14 at 11:18
up vote 6 down vote accepted

The behavior when using curly brackets is exactly the same. However when not using curly brackets but colon instead, the following will not compile:

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

I assume that the reason for preferring elseif over else if is either to be able to switch to and from curly brackets without creating a compile error or, probably more likely, it is a stylistic choice and prefers that only elseif is used.

According to the php documentation:

Nota: 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.

share|improve this answer

Besides this answer, in both PSR2.0 and PEAR coding standards the use of elseif instead of else if is specified so that all control keywords look like single words:

if ($expr1) {
    // if body 
} elseif ($expr2) {
    // elseif body 
} else {
    // else body; 

The keyword elseif SHOULD be used instead of else if so that all control keywords look like single words


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.