The PHP If Statement And The Elseif Clause - Page 2

The PHP If Statement And The Elseif Clause - Page 2
elseif ($current_month == 2) { $current_image = "image_feb.jpg"; }
// It is February(2), set variable to image_feb.jpg

elseif ($current_month == 3) { $current_image = "image_march.jpg"; }
elseif ($current_month == 4) { $current_image = "image_apr.jpg"; }
elseif ($current_month == 5) { $current_image = "image_may.jpg"; }
elseif ($current_month == 6) { $current_image = "image_june.jpg"; }
elseif ($current_month == 7) { $current_image = "image_july.jpg"; }
elseif ($current_month == 8) { $current_image = "image_aug.jpg"; }
elseif ($current_month == 9) { $current_image = "image_sept.jpg"; }
elseif ($current_month == 10) { $current_image = "image_oct.jpg"; }
elseif ($current_month == 11) { $current_image = "image_nov.jpg"; }
elseif ($current_month == 12) { $current_image = "image_dec.jpg"; }

The rest of the code is a series of elseif clauses which sets the $current_image variable to the image corresponding to the current month. As you can see each elseif clause begins with an "elseif". You can also use the syntax "else if" instead. It is a matter of choice. Next in the statement is the expression to be tested which is between the ( and ). Finally the task to be performed if that expression is true is between the { and }. Did you notice that I did not use a default else clause at the end of the sequence? Now all you need to do is add this PHP code inside your HTML webpage where you wish the image to be displayed. Place the PHP code between the opening PHP tag (<?php) and the closing PHP tag (?>) , change the extension of the webpage to .php or .php3 and upload the twelve images (image_jan.jpg … image_dec.jpg) to your server. Here is the complete code including the default code if no expression is true.

<?php
$current_image = "";
$current_month = date(n);

if ($current_month == 1) { $current_image = "image_jan.jpg"; }
elseif ($current_month == 2) { $current_image = "image_feb.jpg"; }
elseif ($current_month == 3) { $current_image = "image_march.jpg"; }
elseif ($current_month == 4) { $current_image = "image_apr.jpg"; }
elseif ($current_month == 5) { $current_image = "image_may.jpg"; }
elseif ($current_month == 6) { $current_image = "image_june.jpg"; }
elseif ($current_month == 7) { $current_image = "image_july.jpg"; }
elseif ($current_month == 8) { $current_image = "image_aug.jpg"; }
elseif ($current_month == 9) { $current_image = "image_sept.jpg"; }
elseif ($current_month == 10) { $current_image = "image_oct.jpg"; }
elseif ($current_month == 11) { $current_image = "image_nov.jpg"; }
elseif ($current_month == 12) { $current_image = "image_dec.jpg"; }
else { $current_image = "image_default.jpg"; }
?>






This site needs an editor - click to learn more!



RSS
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.