Search⌘ K
AI Features

if-else Statement

Explore how to use if-else statements in PHP to create decision branches in your code. Understand syntax, proper use of braces, and how to nest conditionals for complex logic control.

Branches in PHP #

Programming in general often requires a decision or a branch within the code to account for how the code operates under different inputs or conditions.

Within the PHP programming language, the simplest and sometimes the most useful way of creating a branch within your program is through an if-else statement.

if-else #

Let’s start by taking a look at an example which checks if a variable a is greater than or less than b:

PHP
<?php
$a = 50; //change the value of "a" so its less than b in order to execute the else statement
$b = 15;
if ($a > $b)
{
//this code is executed only if $a is greater than $b
echo "a is greater than b";
}
else
{
//this code is executed if the preceding "if" condition evaluated to false
echo "a is less than b";
}
?>

The figure below illustrates how the working of the above code snippet ...