Alternate PHP if statement syntax for view files

We can also write these if/else statements using an alternate syntax. Currently, this is written in one giant PHP code block, but usually when you are intermingling PHP & HTML, you will instead want to use inline PHP blocks with this alternate if/else syntax.

Let’s remove all this code, and write these inline instead.

We’ll open up our PHP tag, write our if $hasPosts line, but instead of a bracket, let’s use a colon. Then, we will close up this PHP tag.

<?php
$title = 'My Blog';
$numPosts = 10;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<?php if ($hasPosts): ?>

<?php endif ?>

To end this control structure, we will open up another PHP tag, then type endif, then close up this PHP tag.

Normally endif always has a semicolon after it, to designate the end of the current line of PHP. Since we are closing up this PHP tag directly after it, we do not need it.

<?php
$title = 'My Blog';
$numPosts = 10;
$hasPosts = $numPosts > 0;
$numPostsDisplay = "\\"$numPosts\\" posts";
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<?php if ($hasPosts): ?>
    Posts exist.
<?php endif ?>

Since our PHP tags have been closed off, that means that we are outside of the area read by the PHP interpreter, so HTML will be outputted. We can now just type or “Posts exist.” text without any echo statements.

When we refresh the page, we’ll see that our code still executes properly.

The else statement works the same way. By opening another PHP tag and writing else and a colon, we can then execute our fallback code that displays our “There are no posts.” message.

If we then update our $numPosts variable to 0 and refresh the page, everything works as expected.

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

This alternate syntax is not used within larger blocks of PHP because it is hard to know when conditional statements start and stop when they don’t have brackets, especially because you can nest multiple if statements together.

Bringing back our “There are exactly 3 posts” message, we can add this check within the $hasPosts conditional logic.

Rather than directly outputting the “Posts exist” text, we will add another if statement. This will check if $numPosts equals 3, and if so, we will output “There are exactly 3 posts”.

If not, we will use an else tag, followed by our standard “Posts exist” text.

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

When we refresh the page, we’ll see that just the “Posts exist” message displays, because it has 10 posts, but the number of posts is 3. If we change $numPosts to 3, it will output the “There are exactly 3 posts.” message.

Note that this has the exact same output as the previous lesson that used an elseif statement, though this uses completely different logic to do so. This proves that there is almost always more than one way to do something, and there are usually many different approaches that can be used to tackle the same task.

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