Skip to Content

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 the script to execute certain actions based on provided criteria or a set of rules. These conditions include string or numeric comparisons, mathematic equations and comparisons, and more.

We'll explore a few different scenarios using many comparison operators including <, >, <=, >=, ==, ===, !=, and !==.

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

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 to the screen:

$fruit = "orange";

if ($fruit == "orange") {
echo "My favorite!";
}

Since the value of our $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 to the screen.

Else

The else statement provides an alternative method when 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." to the screen since the condition evaluates to false:

$fruit = "apple";

if ($fruit == "orange") {
echo "My favorite!";
} else {
echo "Not my favorite.";
}

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

Else If

You can chain and evaluate multiple if statements using elseif, where each statement has its own result. 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."

Evaluating Values with Mismatching Data Types

Back to our comparison operators from earlier, 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 do not, so the statement with the === comparison operator returns false.

Switch/Case

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 and 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 much cleaner and more readable. Our favorite $fruit is an orange, so the string "I love oranges." is what will display to the screen. If any of the other statements evaluate to true, those statements would then be output to the screen.

Notice that we also have a default statement. This works similarly to our else statement 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 statement at the end. This ensures that further processing within the switch block does not execute once the first evaluated statement returns true.

You cannot have duplicate case values when creating switch/case statements. Doing so will result in an error and will halt the processing of your application. Make sure each case value you provide is unique within the same switch statement.

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.

Created: August 30, 2022

Comments

There are no comments yet. Start the conversation!

Add A Comment

Comment Etiquette: Wrap code in a <code> and </code>. Please keep comments on-topic, do not post spam, keep the conversation constructive, and be nice to each other.