Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

If Then Else

A if-then-else command branches the execution based on a condition. When the condition is true, the first branch, called then is executed. When the condition is false, the second branch, called else, is executed.

If-then structures may be chained with the elseif keyword. The else branch is then a new if-then structure, with a second condition.

<?php

    $number = rand(0, 10);
    
    if ($number % 2 === 0) {
        print "$number is even\n";
    } else {
        print "$number is odd\n";
    }

?>

Documentation

See Also