Summary: in this tutorial, you’ll learn about the PHP if...else statement that executes a code block when a condition is true or another code block when the condition is false.
Introduction to PHP if-else statement #
The if statement allows you to execute one or more statements when an expression is true:
<?php
if ( expression ) {
// code block
}Code language: PHP (php)Sometimes, you want to execute another code block if the expression is false. To do that, you add the else clause to the if statement:
<?php
if ( expression ) {
// code block
} else {
// another code block
}Code language: PHP (php)In this syntax, if the expression is true, PHP executes the code block that follows the if clause. If the expression is false, PHP executes the code block that follows the else keyword.
The following flowchart illustrates how the PHP if-else statement works: