The PHP If Statement And The Else Clause

The PHP If  Statement And The Else Clause
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!



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.