208

Are elseif and else if completely synonymous, or is there a difference?

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

While I personally dislike seeing elseif in the code, I just need to know if they're synonymous and the PHP manual isn't the easiest to search.

4
  • 11
    Not every other language uses else if, by the way. For instance, VB also uses ElseIf and Ruby uses elsif. Commented Sep 7, 2010 at 20:37
  • 7
    and shell/bash uses elif
    – knittl
    Commented Sep 7, 2010 at 20:40
  • 8
    python also uses elif
    – gregghz
    Commented Sep 9, 2011 at 16:37
  • Note that the PSR-12 recommand to use elseif instead of else if see documentation
    – Gautier
    Commented Jul 28, 2023 at 12:22

3 Answers 3

251

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;
5
  • 18
    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
    Commented Sep 7, 2010 at 20:36
  • 3
    PHP doesn't treat else if as else { if {, otherwise you wouldn't able to do if (0) { } else if (0) { } else { }... Commented Jul 10, 2015 at 14:05
  • 5
    @GuidoHendriks: it's not that one is treated as the other, it's that they're functionally equivalent. Your example is equivalent to the nested form if (0) { } else { if (0) { } else { } }. Note there's no ambiguity in the last else block, which is always the case for complete branches (every if has an else).
    – outis
    Commented Jul 16, 2015 at 20:55
  • Here's another example for reference php.net/manual/en/control-structures.elseif.php#115851
    – WhyAyala
    Commented Aug 10, 2017 at 14:44
  • With alternate synyax you just have to remember 'colon' and endif of inner if:if ($v1='1'): do_thing(); else: if($v1='b' ): do_another_thing(); else: do_smthing_else(); endif; endif; is the equivalent of; if ($v1='1'): do_thing(); elseif($v1='b' ): do_another_thing(); else: do_smthing_else(); endif;
    – DDS
    Commented Aug 21, 2019 at 15:27
55

The Framework Interoperability Group (FIG) which is made up of members that include the developers of Zend ( https://github.com/php-fig/fig-standards#voting-members ) , put together a series of Standard recommendations (PSR-#).

Zend2 and Symfony2 already follows PSR-0.

There's no hard and fast rules for styles, but you can try and follow as much of PSR-2 as you can.

There's a comment on else if vs elseif in PSR-2:

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

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md#51-if-elseif-else

Some of the recommendations are just that, recommendations. It's up to you whether to use else if or elseif

3
  • 5
    Thank you very much for pointing out the keywords-as-one-word guideline. Commented Dec 14, 2012 at 17:20
  • So the recommondation implies that else if is a single control keyword that just happens to look like multiple words.
    – Robert
    Commented Aug 10, 2021 at 20:00
  • No, the coding guide style recommends using elseif to make all control words "single-words" over else if, which happens to behave exactly in the same way, but breaks the symmetry the author of the guide is looking for. Commented Sep 20, 2022 at 8:29
-2

Yes, "elseif" and "else if" both are synonymous, alike in meaning and significance. You can mix "else if" with "elseif" together.

For Example - We can use "else if" and "elseif" together.
if($condition){
  ...
}
else if($condition){
  ...
}
elseif($condition){
  ...
}

Nested:
if($condition){
  ...
}
else if($condition){
  if($condition){
    ...
  }
  elseif($condition){
    ...
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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