Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are "elseif" and "else if" completely synonymous?

People also ask

What is the difference between Elseif and else if?

There is no difference between “elseif” or “else if” just another way to write it. If statements allow multiple elseifs which can then be followed by else. I prefer using 'else if', but that's just by convention and it tends to make the code look a little neater, for me.

What is the difference between if else and if Elseif else in PHP?

if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions. switch statement - selects one of many blocks of code to be executed.

Does Elseif need end if?

In the multiline syntax, the If statement must be the only statement on the first line. The ElseIf , Else , and End If statements can be preceded only by a line label. The If ... Then ... Else block must end with an End If statement.

Should we use else if?

The main reason to use else if is to avoid excessive indentation. Of course both of the pieces of code above are equivalent (which means it's impossible for the latter to be mandatory other than in style guides).


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;

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