The if Statement
It is often desirable when writing code to perform different actions based on different decision. In PHP this is possible using conditional statements - The if Statement, the if...else statement, and elseif statement.
- if Statement: use this statement to execute a block of code when a condition is true (and another if the condition is not true)
- if...else Statement: use this statement to execute a block of code when a condition is true or to execute another block of code when a condition is false
- elseif Statement: a combination of if and else. It extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. Unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE.
In cases when it is necessary to execute a block of code if a condition is true, the if statement is appropriate.
Following is the correct If Statement Syntax:
if (condition) {
code to be executed
}
Lines of code within the If Statement are enclosed within curly braces ({}). The braces define the beginning (opening brace {) and end (closing brace }) of the If Statement. The following example demonstrates the use of the if statement:
<?php
$number = 5;
if ($number <= 10)
{
echo "The number is less than or equal to 10.";
}
?>
The number is less than or equal to 10.
In the above example, the number 5 is assigned to the variable $number. Next, the PHP script uses the "> =" less than or equal to Comparison Operator to compare the value of $number to the number 10. If the value is less than or equal to 10, the echo statement displays the message "The number is less than or equal to 10" in the browser window. Again, the braces are used to enclose the if statement. The opening brace { appears immediately after the condition statement and the closing brace } appears at the end of the if statement.
Recall that all PHP statements must be properly terminated using the instruction terminator (;):
echo "Number is less than or equal to 10";
In some cases, it may be necessary to provide an alternative message. In the above example, suppose the variable $number contains the number 15 which is greater than 10. An alternate message should be displayed to let the user know that the number is greater than 10. This can be accomplished using the if...else Statement:
<?php
$number = 15;
if ($number <= 10) {
echo "Number is less than or equal to 10.";
}
else {
echo "Number is greater than 10";
}
?>
Number is greater than 10.
In the above example, the number 15 is assigned to the variable $number. Next, the PHP script uses the ">=" less than or equal to Comparison Operator to compare the value of $number to the number 10. If the value is less than or equal to 10, the echo statement displays the message "The number is less than or equal to 10" in the browser window. Now, the else statement has been included to offer an alternative message if the condition in the if statement is not true. The alternate message "Number is greater than 10" is displayed in the browser window if the condition statement returns false. Again, the braces are used to enclose the if statement. The opening brace { appears immediately after the condition statement and the closing brace } appears at the end of the if statement. Braces are also used to enclose the alternate else statement.
A third type of conditional statement is the elseif structure. The elseif statement is a combination of the if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. There may be several elseif structures within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed.
<?php
$number = 15;
if ($number < 10) {
echo "Number is less than 10.";
}
elseif ($number == 10) {
echo "Number is equal to 10.";
}
else {
echo "Number is greater than 10.";
}
?>
Number is greater than 10.
In this example, the number stored in the variable, $number, is compared to 10. First, the if statement checks to see if $number is less than 10. If this statement is true the message, "Number is less than 10" is displayed. Next, an elseif statement is used to determine if $number is equal to 10. If this statement is true, the message "Number is equal to 10 is displayed." The elseif statement is executed ONLY if the if statement returns FALSE. Finally, if the if and elseif statements return FALSE, the else statement is executed and the message "Number is greater than 10" is displayed. Whereas the if statement allows only one condition to be tested, The elseif structure can be used to check multiple conditions.
The following example uses the checkdate() function (section 3-6), the array explode() function (section 3-2), and an if...else statement to check the validity of a date string.
<?php
$orig_date = "09/19/2005";
$date = explode("/", "$orig_date");
$month = $date[0];
$day = $date[1];
$year = $date[2];
$result = checkdate($month, $day, $year);
if ($result == true)
{
echo "Valid Date";
}
else
{
echo "Invalid date";
}
?>
Valid Date
You can use if statements alone or as part of an if...else or if...elseif...else statement. Whichever you choose, you will find this structure to be an invaluable element in your programs.