In PHP the correct is else if or elseif?

12

In PHP, is correct else if or elseif ? What's the difference between them?

The language allows you to write everything together and separate, and apparently the results are identical

    
asked by anonymous 10.04.2015 / 20:50

3 answers

16

As none of the answers spoke of this, I thought it best to include another. It has been said, and it is true, that for practical purposes elseif and else if are equivalent (except in syntax with if(): / endif; , which does not allow else if separated). But why?

You probably already know that it is possible to omit the keys after a if or else if after it only one line comes:

if($condicao)
    echo 'esta linha só executa se passar na condição';
    echo 'esta linha SEMPRE EXECUTA';

or

if($condicao)
    echo 'esta linha só executa se passar na condição';
else
    echo 'esta linha só executa SE NÃO PASSAR na condição';
    echo 'esta linha SEMPRE EXECUTA';

When you use else if , separated, what you have is else that is not followed by keys, with if inside.

An example like this:

if ($a > $b) {
    echo "a is bigger than b";
} else if ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}

Actually, there are two % nested% s , like this:

if ($a > $b) {
    echo "a is bigger than b";
} else 
    if ($a == $b) {
        echo "a is equal to b";
    } else {
        echo "a is smaller than b";
    }
    
10.04.2015 / 22:04
7

According to the PHP online manual , they are only considered equal if used with keys after the condition. If they are used with : (without keys), then separating else if will generate an error.

In the manual site you can find:

  

In PHP, you can write 'else if' (in two words) that the   behavior will be identical to 'elseif' (in a single word).

Example adapted by me:

<?php
/* Escritos juntos: */
if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}

/* Escritos separados: */
if ($a > $b) {
    echo "a is bigger than b";
} else if ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}
?>

Still in the manual site, it is stated that there is only one difference ... (apart from the syntactic difference, -> which for the programmer is something indifferent this part is mine) :

  

Note: Note that elseif and else if will only be considered exactly   same as used with keys as in the example below. When using with   colon (:) to set the if / elseif conditions, you can not   separate else if in two words, or PHP will fail with a   interpretation.

<?php

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


/* Correct Method: */
if($a > $b):
    echo $a." is greater than ".$b;
elseif($a == $b): // Note the combination of the words.
    echo $a." equals ".$b;
else:
    echo $a." is neither greater than or equal to ".$b;
endif;

?>
    
10.04.2015 / 21:31
6
Essentially it's the same thing. Both syntaxes are accepted in most cases. The documentation shows a situation where it can not but is something that a PHP programmer rarely uses. In the syntax that uses : and endif as block delimiters of if does not accept the use of else if .

This can be seen here:

if (true) 
    echo "true";
elseif (false)
    echo "false";
echo "\n";
if (true) 
    echo "true";
else if (false)
    echo "false";
echo "\n";
if (true):
    echo "true";
elseif (false):
    echo "false";
endif;
echo "\n";
if (true):
    echo "true";
else if (false): //isto não compilará
    echo "false";
endif;
echo "\n";
if (true) {
    echo "true";
} elseif (false) {
    echo "false";
}
echo "\n";
if (true) {
    echo "true";
} else if (false) {
    echo "false";
}

See running on ideone .

    
10.04.2015 / 21:29