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…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago