Summary: in this tutorial, you’ll learn about the PHP if elseif statement to execute code blocks based on multiple boolean expressions.
Introduction to the PHP if elseif statement #
The if statement evaluates one expression and executes a code block if the expression is true:
<?php
if (expression) {
statement;
}Code language: PHP (php)The if statement can have one or more optional elseif clauses. The elseif is a combination of if and else:
<?php
if (expression1) {
statement;
} elseif (expression2) {
statement;
} elseif (expression3) {
statement;
}Code language: PHP (php)PHP evaluates the expression1 and execute the code block in the if clause if the expression1 is true.
If the expression1 is false, the PHP evaluates the expression2 in the next elseif clause. If the result is true, then PHP executes the statement in that elseif block. Otherwise, PHP evaluates the expression3.
If the expression3 is true, PHP executes the block that follows the elseif clause. Otherwise, PHP ignores it.
Notice that when an if statement has multiple elseif clauses, the elseif will execute only if the expression in the preceding if or elseif clause evaluates to false.
The following flowchart illustrates how the if elseif statement works: