Archived by PermaMarks.net Personal Web Page Archiver.

Original URL: http://webcache.googleusercontent.com/search?q=cache:EE4iYgs7rl8J:stackoverflow.com/questions/3662412/are-elseif-and-else-if-completely-synonymous+&cd=1&hl=en&ct=clnk&gl=us

Last fetched at 2012-11-15 16:59:21 CST

This is Google's cache of http://stackoverflow.com/questions/3662412/are-elseif-and-else-if-completely-synonymous. It is a snapshot of the page as it appeared on Oct 26, 2012 08:41:20 GMT. The current page could have changed in the meantime. Learn more
Tip: To quickly find your search term on this page, press Ctrl+F or ⌘-F (Mac) and use the find bar.

Text-only version
 
php - Are "elseif" and "else if" completely synonymous? - Stack Overflow

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.

share|improve this question

4  
Not every other language uses else if, by the way. For instance, VB also uses ElseIf and Ruby uses elsif. – Daniel Vandersluis Sep 7 '10 at 20:37
2  
and shell/bash uses elif – knittl Sep 7 '10 at 20:40
1  
python also uses elif – greggory.hz Sep 9 '11 at 16:37
feedback

closed as not constructive by tereško, 一二三, martin clayton, Clyde Lobo, Deanna Sep 11 at 8:35

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

1 Answer

up vote 34 down vote accepted

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; 
share|improve this answer
1  
Sir, I need an example, please. – Theodore R. Smith Sep 7 '10 at 20:25
2  
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 Sep 7 '10 at 20:36
feedback

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