The PHP if
statement lets a script test to see if a certain condition is true, and run a chunk of code if it is. This allows your code to make decisions about what to do next, thereby adding a lot of power to your scripts.
PHP if
syntax
A PHP if
construct takes the following general format:
if ( condition ) {
// This code is run if condition is true
}
// This code is run anyway
Here’s how it works:
- The PHP engine tests the
condition
inside the parentheses. - If the condition is
true
then the PHP engine runs the code inside the braces ({}
), then it starts running any code after the closing brace. - If the condition is
false
then the PHP engine skips the code inside the braces, and starts running any code after the closing brace.
You can use any expression as the condition in an if
statement, as long as it evaluates to either true
or false
. You can use one or more PHP operators to build your condition expression.
Some examples using the if
statement
Here’s a simple example script showing the PHP if
statement in action:
$numWidgets = 3;
if ( $numWidgets > 1 ) {
echo "We have more than 1 widget in stock!<br />";
}
echo "Number of widgets: $numWidgets<br />";
When run, this code displays the following in a Web browser:
We have more than 1 widget in stock! Number of widgets: 3
The script sets a $numWidgets
variable to 3, then tests the variable to see if its value is greater than 1. Since it is, the code inside the braces is run and the message “We have more than 1 widget in stock!” is displayed. Finally the code after the if block is run, displaying “Number of widgets: 3”.
What happens if $numWidgets
isn’t greater than 1?
$numWidgets = 0;
if ( $numWidgets > 1 ) {
echo "We have more than 1 widget in stock!<br />";
}
echo "Number of widgets: $numWidgets<br />";
This code displays:
Number of widgets: 0
Since $numWidgets
isn’t greater than 1, the code inside the if
block is skipped and the first message isn’t displayed.
Choosing between chunks of code with if ... else
You can enhance the if
statement with the else
statement. This lets you run one chunk of code if a condition is true, or a different chunk of code if the condition is false:
if ( condition ) {
// This code is run if condition is true
} else {
// This code is run if condition is false
}
// This code is run anyway
Here’s an example that uses if ... else
:
$numWidgets = 0;
if ( $numWidgets >= 1 ) {
echo "We have at least 1 widget in stock<br />";
} else {
echo "We have less than 1 widget in stock<br />";
}
echo "Number of widgets: $numWidgets<br />";
This code displays:
We have less than 1 widget in stock Number of widgets: 0
$numWidgets
is less than 1, so the condition $numWidgets >= 1
is false
. Therefore the PHP engine runs the code inside the else
block, displaying the message "We have less than 1 widget in stock"
.
You now know how to use the PHP if
statement to make decisions about which piece of code to run. You’ve also seen how to use the else
statement to choose between two different blocks of code. Happy coding!
Leave a Reply