AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

PHP if…else: control structures and program logic

PARTAGEZ

The PHP if…else function allows you to control how your code flows. Define the instructions that must be executed and the conditions for their execution to control your program precisely.

PHP if…else function: what is it?

The PHP if…else function is a basic control structure that allows taking conditional decisions within a program. It takes care of checking a specific or nested condition with PHP operators, and executing the corresponding block of code if it is considered “true” (“ true “). If the condition is judged to be “false” (“ false “), another block of code is then selected. This allows you to define behaviors for different scenarios, particularly when calling PHP functions or arithmetic calculations.

What does the syntax of the PHP if…else function look like?

The syntax for a simple if…else statement in PHP looks like the following:

if (Condition) {
    // Code à exécuter si la condition est (true).
} else {
    // Code à exécuter si la condition est (false).
}

php

  • if (condition) : This is where the condition to be checked is indicated. If this turns out to be true (“ true “), the code is then executed in the first block (just after the opening brace);
  • {…} : the code block called if the condition is true is located between these two braces;
  • else : this part of the syntax is optional. If the condition indicated in “if” turns out to be false (“ false ), the code contained in the “else” block is then selected.

Discover Deploy Now, an IONOS solution allowing you to easily manage your development projects on a stable hosting platform. Thanks to its rapid integration with your GitHub repository, efficiently deploy your code updates without much effort. With Deploy Now, engage in your development work securely and conveniently.

“elseif”: what is it?

L'elseif statement is an extension of the PHP if…else function. It allows several conditions to be evaluated, with a specific code block assigned to each of them.

if (Condition1) {
    // Code à exécuter si la condition1 est vraie
} elseif (Condition2) {
    // Code à exécuter si la condition2 est vraie
} else {
    // Code à exécuter si aucune des conditions n'est vraie
}

php

It is also possible to write the PHP if…else statement more compactlywithout the braces:

$var = 5;
if ($var > 5):
    echo "var est plus grand que 5";
elseif ($var == 5):
    echo "var est égal à 5";
else:
    echo "var est inférieur à 5";
endif;

php

With this version, your if statement must end with endif.

For other basic information on PHP programming, you can check out our PHP tutorial. We also advise you to take a look at our comparison articles between PHP and Python and between PHP and JavaScript, so you can discover the advantages and disadvantages of each language.

Free IONOS API

Use the IONOS API at no additional cost to retrieve or update your domain, DNS and SSL data.

DNS Records

SSL Administration

API Documentation

Application examples of the PHP if…else function

You can use PHP if…else statements flexibly and in different forms for applications.

Shorthand for the PHP if…else function

The short form of the PHP if…else function allows you to create a simple conditional statement that fits on a single line. It is often referred to asternary operatorbecause three different elements make it up: the condition, the value returned if the condition is true and the value returned if it is found to be false.

$âge = 20;
$statut = ($âge >= 18) ? "majeur" : "mineur";

php

For this example, we want to check if the variable $âge is much greater than or equal to 18. As here, it is greater, the value “major” is then assigned to the variable $statut.

Conditional logic for database queries

If you use PHP to retrieve information from a MySQL database, you can transform the corresponding data into instances of PHP classes and make use of the conditional logic :

class Utilisateur {
    public $nom;
    public $âge;

    public function __construct($nom, $âge) {
        $this->nom = $nom;
        $this->âge = $âge;
    }

    public function estmajeur() {
        if ($this->âge >= 18) {
            return true;
        } else {
            return false;
        }
    }
}

php

We start by defining the “User” class with the “name” and “age” properties and the “ismajor()” method.

$ListeUtilisateur = array();

while ($row = mysqli_fetch_assoc($resultat)) {
    $utilisateur = new Utilisateur($row['nom'], $row['âge']);
    $ListeUtilisateur[] = $utilisateur;

    if ($utilisateur->estmajeur()) {
        echo $utilisateur->nom . " est majeur.<br>";
    } else {
        echo $utilisateur->nom . " est mineur.<br>";
    }
}

php

We declare a variable in the array empty, $ListeUtilisateur, which will allow us to store user data. Using PHP loops such as whilewe can now iterate through the resulting records. In the loop whilewe create an object of the “User” class for each user record, which we add to the list $ListeUtilisateur. Finally, we use the PHP if…else statement to check the user's age. Depending on whether it is an adult or not, we then display the corresponding message.

IONOS S3 Object Storage

IONOS S3 Object Storage is ideal for backups and archiving company data. You can store any amount of static data at low cost.

Scalable

Economic

Practical

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact

Suivez-nous:

© 2024 AMZ DIGICOM All Rights Reserved