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
Action Movies
Bible Basics
Houseplants
Romance Movies
Creativity
Family Travel
Southwest USA


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 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.

<html>
<head>
<title></title>
</head>
<body>

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

</body>
</html>




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


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


Content copyright © 2014 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
Poetry
Daily
Weekly
Monthly
Less than Monthly



BellaOnline on Facebook
g


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


BellaOnline Editor