Rocio  O'Keefe

Rocio O'Keefe


The PHP If Statement

This is the first of a series of posts I will write that go back to basics and introduce the fundamentals of PHP. In this article I’m going to start with the PHP if statement.

The if statement is pretty simple right. If this then do that, else do something else. Sorted? There is a bit more to it than that…

The purpose of an if statement is to have a condition, or test, and if that passes then run the code within that section. If it doesn’t pass then that code doesn’t get run. You can also specify an else, which will run if the condition or test fails. It is very simple in principle but is a very powerful tool.

There is actually a bit more to consider about the humble if statement in PHP. Let’s start with the syntax.


There are a few different ways you can write an if statement in PHP and end up with the same result. Below provides a few different ways you might see if statements in PHP.

With Curly Brackets

This example uses brackets to help split up the sections of the if statement. If you have an IDE or a good text editor you can normally click on the opening bracket and it will highlight the opening and closing bracket so you can see where each section starts and ends.

$myVar = true;

if ($myVar === true) {
    echo "It is true";
} else {
    echo "It is false";

#php #code #beginner

The PHP If Statement