The following code shows an example of Using Nested If in PHP.

As can be seen in the following code, we use the nested-if statement to find the largest of four numbers.

<?php
echo '<h1>Example Demonstrating Nested-if Statement in PHP</h1>';
echo '<h2>Find Largest of four numbers: [31, 12, 90, 56]</h2>';
$a=31;
$b=12;
$c=90;
$d=56;

if($a>$b)
{
    if($a>$c)
    {
        if($a>$d)
        {
            echo '<h2>'.$a.' is Largest!</h2>';
        }
        else
        {
            echo '<h2>'.$d.' is Largest!</h2>';
        }
    }
    else
    {
        if($c>$d)
        {
            echo '<h2>'.$c.' is Largest!</h2>';
        }
        else
        {
            echo '<h2>'.$d.' is Largest!</h2>';
        }
    }
}
else
{
    if($b>$c)
    {
        if($b>$d)
        {
            echo '<h2>'.$b.' is Largest!</h2>';
        }
        else
        {
            echo '<h2>'.$d.' is Largest!</h2>';
        }
    }
    else
    {
       if($c>$d)
        {
            echo '<h2>'.$c.' is Largest!</h2>';
        }
        else
        {
            echo '<h2>'.$d.' is Largest!</h2>';
        }
    }
}

?>

Output

Largest of Four Numbers Using Nested-If Statement in PHP
Largest of Four Numbers Using Nested-If Statement in PHP

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

PHP Practice Questions