Write multiple conditions to execute alternate PHP code

We can also supply an “else” condition, which will execute this block of code whenever the checked condition is false. We can do this by typing else after our closing bracket tag, then another open & close bracket.

If we change our $numPosts value to 0, save, and refresh the page, we will see that it triggers this else condition, because $hasPosts will evaluate to false.

<?php
$title = 'My Blog';
$numPosts = 0;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<?php
if ($hasPosts) {
    echo 'Posts exist.';
} else {
    echo 'There are no posts.';
}
?>

We can also use elseif if we had multiple conditions to check. For example, let’s say wanted to execute a bit of code, but only if the number of posts is exactly 3. We can add another elseif to this if/else block, with another open & close bracket, and specify the condition for this code. $numPosts == 3.

<?php
$title = 'My Blog';
$numPosts = 3;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<?php
if ($hasPosts) {
    echo 'Posts exist.';
} elseif ($numPosts == 3) {
    echo 'There are exactly 3 posts.';
} else {
    echo 'There are no posts.';
}
?>

If update numPosts to 3, save & refresh, we’ll see something interesting. Since PHP evaluates code from top→down, this means that the first condition the matches a truthy condition will be executed. Even though our $numPosts equals 3, since $hasPosts is the first condition to be evaluated, this is the code that gets executed.

In order for our code to execute as we expect, our $numPosts == 3 check would need to come first. So, let’s switch around these conditions.

<?php
$title = 'My Blog';
$numPosts = 3;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<?php
if ($numPosts == 3) {
    echo 'There are exactly 3 posts.';
} elseif ($hasPosts) {
    echo 'Posts exist.';
} else {
    echo 'There are no posts.';
}
?>

When we refresh, we will now see our “There are exactly 3 posts” message. If we set $numPosts to 10, we will see our “Posts exist” message, and if we set it to 0, we will see “There are no posts”.

<?php
$title = 'My Blog';
$numPosts = 10;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<?php
if ($numPosts == 3) {
    echo 'There are exactly 3 posts.';
} elseif ($hasPosts) {
    echo 'Posts exist.';
} else {
    echo 'There are no posts.';
}
?>

A quick note about the double equal sign == comparison operator. This double equal sign is considered a “weak check”. This is because of $numPosts equals a 3 string '3', it will still evaluate to true. A double equal sign will only check the result of the condition, and not that the data types of the values on both sides of the condition.

In order to force a “strict check”, use a triple equal sign ===. This will check not only the value of each side of the condition, but also make sure the data types match. This is the preferred method of using comparison operators, and you should really never use the double equal sign operand unless you had a very specific reason to do so (and it’s probably not a good one).

Extra lesson content locked
Enroll to access source code, comments & track progress.
Enroll for Free to Unlock