How to Create Conditional Statements in PHP
In this tutorial, you'll learn how to create conditional statements in PHP. Conditional statements are expressions that tell a script to execute certain actions based on provided criteria or a set of rules. These conditions include string or numeric comparisons, mathematic equations, and more.
If Statements
An if
statement processes the contents between the curly brackets {
and }
if the provided statement between the parenthesis evaluates to true.
For example, if our favorite fruit is an orange, we'll evaluate and output it on screen:
$fruit = "orange";
if ($fruit == "orange") {
echo "My favorite!";
}
Since the value of the $fruit
variable is orange, the string "My favorite!" will appear on the screen.
You could create a similar conditional statement by evaluating integer values:
$oranges = 2;
if ($oranges > 1) {
echo "I have multiple oranges.";
}
Since the value of $oranges
is greater than 1, the string "I have multiple oranges." will display on screen.
If...Else Statements
The if...else
statement provides an alternative method when the result of the if
statement evaluates to false.
For example, if our favorite fruit is an orange, but our $fruit
variable is set to apple, we'll display the text "Not my favorite." on screen since the condition evaluates to false:
$fruit = "apple";
if ($fruit == "orange") {
echo "My favorite!";
} else {
echo "Not my favorite.";
}
We can create a similar conditional statement by evaluating integer values:
$oranges = 1;
if ($oranges > 1) {
echo "I have multiple oranges.";
} else {
echo "I only have one orange.";
}
Which outputs "I only have one orange." since our initial if
statement evaluates to false.
If...Else If...Else Statements
You can chain and evaluate multiple if
statements using elseif
. If all if
statements evaluate to false, then we use the result in else
:
$fruit = "mango";
if ($fruit == "orange") {
echo "I love oranges.";
} else if ($fruit == "apple") {
echo "I love apples.";
} else {
echo "I love a different fruit.";
}
Since the value of $fruit
is neither orange nor apple, the output will be "I love a different fruit."
Switch/Case Statements
There may be cases (no pun intended!) where you'll have many statements to evaluate in one go. Rather than creating a long list of if...elseif
statements, PHP provides a different set of commands, switch
and case
, that allow for a much cleaner approach.
The switch
statement will contain our conditional statement and each case
statement we provide will contain different conditions we're evaluating. Here's a quick example showing how this works:
$fruit = "orange";
switch ($fruit) {
case "apple":
echo "I love apples.";
break;
case "orange":
echo "I love oranges.";
break;
default:
echo "I love other fruits.";
break;
}
As you can see, this code is already much cleaner and more readable. Our favorite $fruit
is an orange, so the string "I love oranges." is what will display on screen. If any of the other statements evaluate to true, those statements would then be output.
Notice that we also have a default
statement. This works similarly to else
where, if all possible outcomes are false, the default
statement is what remains to be true.
You'll also notice that each case
statement contains a break
at the end. This ensures that further processing within the switch
block doesn't execute once the first evaluated statement returns true.
You can't define duplicatecase
values when creating switch/case statements. Doing so will result in an error and halt the processing of your application. Make sure eachcase
value you provide is unique within the sameswitch
statement.
Comparison Operators
There are many different ways to compare strings and numeric values. These comparisons are evaluated using shorthand operators. Here are the most common operators you can use in your statements:
<
- less than>
- greater than<=
- less than or equal to>=
- greater than or equal to==
- value is equal to===
- value and data type are exact matches!=
- value is not equal to!==
- value and/or data type are not exact matches
Evaluating Values with Mismatching Data Types
With these comparison operators, let's test a few conditions with matching values and mismatching data types:
$apples = "2";
$oranges = 2;
if ($apples == $oranges) {
echo "Match";
} else {
echo "Mismatch";
}
Although the two variables $apples
and $oranges
are two different data types, each of their values is 2, resulting in a match because the ==
comparison operator is only checking each variable's values.
Now, we'll flip the narrative a bit, changing the comparison operator to ===
:
$apples = "2";
$oranges = 2;
if ($apples === $oranges) {
echo "Match";
} else {
echo "Mismatch";
}
Although the values of the two variables match, the datatypes don't, so the statement with the ===
comparison operator returns false.
Conclusion
Conditional statements in PHP are a great help when evaluating strings, numbers, or any other data type. The above examples illustrate possible use cases and best practices for each method.
Comments
There are no comments yet. Start the conversation!