PHP if-else gives you control over the flow of your code. You can specify which state­ments are to be executed under which con­di­tions, allowing you to control your program more precisely.

What is PHP if-else?

PHP if-else is a basic control structure that allows you to make con­di­tion­al decisions in a program. It checks a specific condition or a condition nested with PHP operators and executes a block of code if it evaluates to true. If the condition is false, an al­tern­at­ive code block is selected. This allows you to define be­ha­viours for different scenarios. These can be, for example, calling PHP functions or arith­met­ic cal­cu­la­tions.

What the syntax of PHP if-else looks like

The syntax of a simple if-else statement in PHP is as follows:

if (condition) {
    // Code to be executed if the condition is true.
} else {
    // Code to be executed if the condition is false.
}
php
  • if (condition): The condition to be checked is specified here. If this condition is true, the code in the first block (after the opening curly bracket) is executed.
  • { … }: These curly brackets contain the code block that is called if the condition is true.
  • else: This part is optional. If the condition in the If part is false, the code in the Else block is selected.
Tip

Discover Deploy Now from IONOS to run your de­vel­op­ment projects on a stable hosting platform. Rapid in­teg­ra­tion with your GitHub re­pos­it­ory enables efficient de­ploy­ment of your code updates, without much hassle. With Deploy Now, you can carry out your de­vel­op­ment work securely and con­veni­ently.

What is elseif?

The elseif statement is an extension of PHP if-else. It’s used to evaluate several con­di­tions. Each is assigned its own code block:

if (condition1) {
    // Code that is executed if condition1 is true
} elseif (condition2) {
    // Code that is executed if condition2 is true
} else {
    // Code that is executed if none of the conditions is true}
php

There is an al­tern­at­ive, more compact notation for PHP if-elseif without curly brackets:

$var = 5;
if ($var > 5):
    echo "var is greater than 5";
elseif ($var == 5):
    echo "var is 5";
else:
    echo "var is smaller than 5";
endif;
php

In this version, you must end the If statement with endif.

Tip

Find out more about PHP pro­gram­ming in our PHP tutorial. We also recommend that you take a look at the com­par­is­ons of PHP vs Python and PHP vs JavaS­cript to learn about the ad­vant­ages and dis­ad­vant­ages of each language.

IONOS Developer API
Manage your hosting products through our powerful API
  • DNS man­age­ment
  • Easy SSL admin
  • API doc­u­ment­a­tion

Examples for using if-else in PHP

PHP if-else state­ments can be used flexibly and in different forms for ap­plic­a­tions.

PHP if-else shorthand

The PHP if-else shorthand allows you to represent a simple con­di­tion­al statement in a single line. It’s often referred to as a Ternary Operator because it has three parts: the condition, the value that is returned if the condition is true, and another value if it’s false.

$age = 20;
$status = ($age >= 18) ? "adult" : "minor";
php

In this example, we check whether the variable $age is greater than or equal to 18. Since it’s greater, the value ‘adult’ is assigned to the variable $status.

Con­di­tion­al logic for database queries

When re­triev­ing in­form­a­tion from a MySQL database using PHP, you can convert the data into instances of PHP classes and use con­di­tion­al logic:

class user {
    public $name;
    public $age;
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
    public function isadult () {
        if ($this->age >= 18) {
            return true;
        } else {
            return false;
        }
    }
}
php

First, we define the Class User with the prop­er­ties ‘name’ and ‘age’ and the method ‘is adult()’.

$userlist = array();
while ($row = mysqli_fetch_assoc($result)) {
    $user = new user($row['name'], $row['age']);
    $userlist[] = $user;
    if ($user->isadult ()) {
        echo $user->name . " is adult.<br>";
    } else {
        echo $user->name . " is a minor.<br>";
    }
}
php

We declare an empty array variable $userList to store user data. With PHP loops like while we can iterate through the result records. In the while loop, we create an object of class ‘user’ for each user record and add it to the $userList. Finally, we use PHP if-else to check whether the user is of legal age or not and output a cor­res­pond­ing message.

IONOS Cloud Object Storage
Cloud storage at an un­beat­able price

Cost-effective, scalable storage that in­teg­rates into your ap­plic­a­tion scenarios. Protect your data with highly secure servers and in­di­vidu­al access control.

Go to Main Menu