In PHP the correct is Else if or elseif?

Asked

Viewed 2,003 times

15

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

The language allows writing everything together and separately, and apparently the results are identical

  • I understood your question. I separated 2 references of the question marked as duplicate: Ref 1, Ref 2.

3 answers

21


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

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

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, separate, what you have is a else which is not followed by keys, with a if within.

Such an example:

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 ifs nested, thus:

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";
    }
  • 1

    now it was clear p/ me

8

Essentially the same thing. The two syntaxes are accepted in most cases. A documentation shows a situation where it cannot but is something that rarely a PHP programmer uses. In the syntax that uses the : and endif as block delimiters of the 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";
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

8

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

On the manual website 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 on the manual website, it is stated that there is a single difference... (beyond the syntactic difference, -> that for the programmer is something indifferent <- this part is mine):

Note: Note that elseif and Else if will only be considered exactly the same if used with keys as in the example below. When using with two points (:) to define the if/elseif conditions, you cannot separate Else if into 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;

?>
  • When you speak of syntactic difference, you mean this?

  • It wasn’t that. I was referring to the syntactic difference between the else if and the elseif... this difference can only be noticed by the PHP parser in your internal parse process.

  • Yeah, but I don’t think that was clear to the AP yet. I posted an answer dealing with the subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.