logo
g Text Version
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Sports
Travel & Culture
TV & Movies

dailyclick
Bored? Games!
Nutrition
Postcards
Take a Quiz
Rate My Photo

new
Business Travel
Herbs
Healthy Foods
Classical Music
Spirituality
Judaism
Disabilities


dailyclick
All times in EST

Low Carb: 8:00 PM

Full Schedule
g
g PHP Site

BellaOnline's PHP Editor

g

The PHP If Statement And The Elseif Clause - Page 2

Guest Author - Diane Cipollo

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


Add The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 to Twitter Add The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 to Facebook Add The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 to MySpace Add The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 to Del.icio.us Digg The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 Add The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 to Yahoo My Web Add The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 to Google Bookmarks Add The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 to Stumbleupon Add The+PHP+If+Statement+And+The+Elseif+Clause+%2D+Page+2 to Reddit



For FREE email updates, subscribe to the PHP Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor


Content copyright © 2012 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.

g


g features
Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter


vote
Fav Social Network
Facebook
Twitter
Google+
other / none



BellaOnline on Facebook
g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2012 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor