Tech Musings

Wednesday, August 01, 2007

PHP elseif statements

This has gotten me more than once. It seems like I always get one of these

Parse error: parse error, unexpected T_ELSEIF in mypath/to/the_page.php on line 534

when I craft my else if statements. Undoubtedly, it's because I surround each else if statement within its own set of PHP <? ?> delimiters.

For example, this code will throw the error:

<? $time = date("H:i:s"); ?>
<? if ($time >= "01:00:00" && $time <= "11:59:59") { echo "Good Morning, "; } ?>
<? elseif ($time >= "12:00:00" && $time <= "18:29:59") { echo "Good Afternoon, "; } ?>
<? elseif ($time >= "18:30:00" && $time <= "24:00:00") { echo "Good Evening, "; } ?>
<? else { echo "Hi, "; } ?>
<? echo $row_login['people_firstname']; ?>

whereas the following syntax is correct:

<? $time = date("H:i:s"); ?>
<?
if ($time >= "01:00:00" && $time <= "11:59:59") { echo "Good Morning, "; }
elseif ($time >= "12:00:00" && $time <= "18:29:59") { echo "Good Afternoon, "; }
elseif ($time >= "18:30:00" && $time <= "24:00:00") { echo "Good Evening, "; }
else { echo "Hi, "; }
?>
<? echo $row_login['people_firstname']; ?>

The elseif statement differs from nested if statements that should be contained within the their own curly braces!

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home