Categories: Development

Example nested if else statement in PHP

Nested PHP if and if else statements can be confusing however if following the correct formatting and PHP basics they can be a very useful piece of code.

In the example below i define $var as 88 and do some comparisons to find where it sits in between higher and lower numbers.

$var = 88;
if ($var >= 100) {
    if ($var <= 200) {
        echo "Var is in between 100 and 200";
    } else if ($var <= 500) {
        echo "Var is in between 200 and 500";
    } else if ($var <= 900) {
        echo "Var is in between 500 and 900";
    }
} else if ($var <= 100) {
    if ($var >= 50) {
        if ($var <= 90) {
            if ($var >= 80) {
                echo "Var in between 80 and 90";//this will be what gets echoed
            }
            else if ($var >= 89) {
                echo "Var in between 89 and 90";
            }
        }
    }
}

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago