g Text Version
Beauty & Self
Books & Music
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
News & Politics
Religion & Spirituality
Travel & Culture
TV & Movies

Bored? Games!
Take a Quiz
Rate My Photo

Home Improvement
Women's Fashion
Small Office/Home Office
Holiday/Seasonal Cooking
Crafts for Kids

All times in EST

Full Schedule
g PHP Site

BellaOnline's PHP Editor


The PHP If Statement And The Else Clause

Guest Author - Diane Cipollo

One of the most frequently used piece of PHP code is the if statement. The PHP if statement and the else clause make it possible for your programs to ask questions and make decisions based on the answer.

The if statement and the else clause

if (this expression is true) { perform this code }
else { perform this default code if expression is not true }

For example, let's say that you have a small business and that you only sell Christmas items. Therefore it would not be cost effective to be "open for business" all year long. So you want to make your online store available to your customers only during the months of November and December. You could code your PHP program to test for the current month and if the month is November or December the program will write the words "Welcome and Merry Christmas" at the top of your webpage. Let's take a look at the code.

$current_month = date(n);
if ( $current_month > 10 )
{ echo "Welcome and Merry Christmas"; }

The first line of code finds the current month of the year which will be a numerical value of 1 through 12. The $current_month variable is then set to this value. The second line says that if the current month is greater then 10 (October) then write "Welcome and Merry Christmas". As you can see the if part of the statement begins with an "if" and the testing part of the if statement is placed between the ( and ). The result of this test can be either true or false. The task to be performed if the condition is true is placed between the { and }.

But what if the condition is false? That is, what if it is not November or December? You will need to code your program to handle this possibility also. Let's have the program write "Sorry, we are closed until November 1st". This is the else clause. The else clause added to the statement handles the possibility that the result of the test is false. Here is this part of the code.

else { echo "Sorry, we are closed until November 1st"; }

You can see that "else" starts this part of the code and the task to be performed is placed between the second pair of { and }.

Now where will you put this PHP code? You will put this code right inside your HTML webpage code at the spot where you want the special greeting to be. But you must place this code between the opening PHP tag (<?php) and the closing PHP tag (?>) which will alert the server that this is PHP code and not HTML code. Finally you will use the .php or .php3 extension for this webpage. Here is the entire code.


$current_month = date(n);
if ( $current_month > 10 )
{ echo "Welcome and Merry Christmas"; }
else { echo "Sorry, we are closed until November 1st"; }


This site needs an editor - click to learn more!

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

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

For FREE email updates, subscribe to the PHP Newsletter

Past Issues

Printer Friendly
tell friend
Tell a Friend
Email Editor

Content copyright © 2015 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 features
Archives | Site Map


Past Issues

Less than Monthly

BellaOnline on Facebook

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

BellaOnline Editor