All Courses > Up and Running > PHP if()-Statements

PHP if()-Statements

The if()-statement allows us, for the first time, to run code only under certain conditions.

Our first element of control flow is the if()-statement, which allows us, for the first time, to run code only under certain conditions. In some sense, this ability is the heart of programming, so you’re learning a very powerful piece of the puzzle.

if()-Statement Syntax

Here’s how an if()-statement works:

if ( /* A condition that will evaluate to true or false */ ) :
	/* Code that will only run if the condition above is true */
endif;

Will the code inside an if()-statement run, or won’t it? The answer is always maybe—that’s the point of if()-statements.

So will the code inside an if()-statement run, or won’t it? The answer is always maybe—that’s the point and the power of if()-statements.

The code inside an if()-statement will run if (and only if) the condition we give it evaluates to true—in other words, if PHP thinks that condition is true rather than false.

Let’s look at some real-life examples to get a firm sense of how this works.

Example if()-Statement

The below examples use [WordPress’s conditional tags] to run only if the content currently being displayed is a Page, is an archive of type Product, and so on. You’ll see if()-statements very like this in theme files (especially index.php and functions.php) as well as plugin files.

/* is_singular( 'testimonial' ) will evaluate to true if and only if
what we're currently displaying is a single post of type Testimonial */
if ( is_singular( 'testimonial' ) ) :
	/* This code will run only if WordPress is currently displaying a singular Testimonial */
	echo '<h1>A piece of great customer feedback:</h1>';
endif;

/* Code out here will run normally; we're outside of the if()-statement */
echo 'This code will run no matter what.';

if()-Statements Including else() and elseif()

We often want multiple options in an if()-statement: a cascade of possibilities, with code for each. We do this by adding two pieces of PHP syntax: else and elseif. Let’s start with else:

PHP if()-Statement with else()

else() is an if()-statement’s flip side: it will always run only when the if()-statement itself doesn’t.

else() acts as an if()-statement’s flip side: it will always run only when the if()-statement itself doesn’t. Together, an “if-else” statement guarantees that one or another possibility will always run—but never both.

Here’s an example of an if()-statement with else(), also called an “if-else” statement:

/* is_singular( 'product' ) will evaluate to true if and only if
what we're currently displaying is a single post of type Product */
if ( is_singular( 'product' ) ) :
	/* This code will run only if WordPress is currently displaying a singular Product */
	echo 'Thanks for viewing this product! Happy shopping.';
else :
	/* This code will run only if WordPress is NOT currently displaying a singular Product */
	echo 'Like our site? Go browse our products!';
endif;

/* Code out here will run normally; we're outside of the if()-statement entirely */
echo 'This code will run no matter what.';

Does every if()-statement need an else()-statement? No: in many cases, like the first one in this chapter, if the if()-statement is false we want to just move on without executing any code in particular. In those cases, there’s no need for else(), which is only needed when there’s specific code you want to run only when the if()-statement evaluates to false.

elseif()

elseif is an operator that comes after an initial if-statement. The code inside an elseif will only run if both of two things are true:

  1. The initial if()-statement, and any following elseif()s that are above the current one, are all false.
  2. The elseif()‘s own condition is true.
/* is_archive( 'post' ) will evaluate to true only if
what we're currently displaying is an archive page for Posts */
if ( is_archive( 'post' ) ) :
	/* This code will run only if WordPress is currently displaying a Posts archive page */
	echo 'Enjoy browsing our articles!';
elseif ( is_archive( 'product' ) ) :
	/* This code will run only if WordPress *is not* currently displaying a Posts archive page,
	and *is* currently displaying a Products archive page */
	echo 'Enjoy browsing our products!';
endif;

In the example above, PHP will check first the original if()-statement, and run the code inside if it evaluates to true. Only then will it check the elseif() statement, running that code if it evaluates to true or simply moving on if not.

Using elseif() and else() Together

In the example above, the two options are mutually exclusive: no page in WordPress will ever be a Posts archive page and simultaneously a Products archive page. However, you can also write elseif() in cases that are not mutually exclusive. Below is an example that asks first if what’s being viewed is the site’s front page, then if it is a Page at all.

One use of elseif() is as a less specific “fallback” for the option asked about in if().

These options aren’t mutually exclusive: something can be both a Page and the site’s front page. In this case, elseif() works like a less specific “fallback” for the option asked about in if().

And in this spirit, we’ll also add an else() to catch all other possibilities that neither the if() nor the elseif() catches. The resulting code looks as follows:

/* is_front_page() will evaluate to true only if
what we're currently displaying is the website's static front page */
if ( is_front_page() ) :
	echo 'We\'re on the homepage!';
/* is_page() will evaluate to true only if
what we're currently displaying is a single post of type Page */
elseif ( is_page() ) :
	echo 'We\'re not on the homepage, but we are on a page.';
else :
	echo 'We\'re not on a page, but I just wanted to print something.';
endif;

Note the Two Syntax Options for if()-Statements

Now that you know how to write if()-statements, here’s a detail to watch for: there are actually two accepted forms of if()-statement syntax in PHP: what we’ll call “verbose” syntax, which we’ve been using throughout these chapter, and “standard” syntax, which uses curly brackets ({}).

These two styles work identically. Here’s the example above in the standard curly brace syntax:

/* is_front_page() will evaluate to true only if
what we're currently displaying is the website's static front page */
if ( is_front_page() ) {
	echo 'We\'re on the homepage!';
/* is_page() will evaluate to true only if
what we're currently displaying is a single post of type Page */
} elseif ( is_page() ) {
	echo 'We\'re not on the homepage, but we are on a page.';
} else {
	echo 'We\'re not on a page, but I just wanted to print something.';
}

These two styles work identically, and the only benefit is personal preference: which one you, the programmer, feel is easier to read and write. The WordPress PHP coding standards are fine with both options.

In our opinion, the verbose syntax is good for clarity, especially when you’re moving in and out of HTML often (that is, opening and closing <?php ?> tags often). However, most PHP code in WordPress and elsewhere will use the standard syntax; using only the verbose option is unusual.

You’ll see this note again when we cover [[[loops]]], which have very similar paired syntax options. And to get you familiar with both styles, we’ll be writing some chapters in one syntax, and some chapters in the other.