One function of PHP that I like to use is the colon-style if statement (I don't know what it's actually called.)
<?php if(something):?>
<html stuff>
<?php endif;?>
But I recently tried to do this with multiple cases:
<?php if(something):?>
<html stuff>
<?php else if(something):?>
<html stuff>
<?php endif;?>
And I get an error on the third line (the else if
):
Unexpected T_IF
Is it possible to make an if-else if this way?
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.
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.
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.
The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.
I was able to get it to work.
In this very specific case, else if
is not synonymous with elseif
.
Substituting elseif
for else if
fixes the issue.
<?php if(something):?>
<html stuff>
<?php elseif(something):?>
<html stuff>
<?php endif;?>
From PHP.net:
Note: 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With