# elseif/else if

Domains:

(PHP 4, PHP 5, PHP 7)

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:

<?php
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";
}
?>


There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to TRUE would 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.

The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.

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.

<?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;

?>

Page structure
Terms
{"ft_lang_label":"__label__en","ft_lang_prob":0.8362513,"math_prob":0.9647643,"size":1847,"snap":"2021-31-2021-39","text_gpt3_token_len":441,"char_repetition_ratio":0.13130765,"word_repetition_ratio":0.052941177,"special_character_ratio":0.2669193,"punctuation_ratio":0.14814815,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9904713,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-18T19:22:03Z\",\"WARC-Record-ID\":\"<urn:uuid:2c2415c6-0e13-40ff-a137-34074b3743dd>\",\"Content-Length\":\"20349\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bbf6e737-786d-4bf8-8ab8-9df4dcf61844>\",\"WARC-Concurrent-To\":\"<urn:uuid:d5c2bae5-3b5f-4c89-a41d-c51912571d8a>\",\"WARC-IP-Address\":\"185.68.16.163\",\"WARC-Target-URI\":\"http://semantic-portal.net/php-language-reference-control-structures-elseif\",\"WARC-Payload-Digest\":\"sha1:G4XMRWOWGS5ZHIRRLMZQNCJRO6OO7SHV\",\"WARC-Block-Digest\":\"sha1:IGPKL7KWJQHHR5REXQPQV2DLJP2BIS6L\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056572.96_warc_CC-MAIN-20210918184640-20210918214640-00255.warc.gz\"}"}