PHP if..else and elseif: Explained with visual examples

Purpose of if..else in PHP

The if else is a decision-making statement in PHP and many other programming languages.

The PHP if statement will execute one block of code between two or many options – so taking the decision based at certain given criteria.

Syntax of PHP if statement

If there are only two options then you may use simply if with the else statement. If the expression in small brackets is true, it will execute the block inside the if statement, otherwise, the else block of code will execute.

Note: You may also use only if without the else statement

If options are more then use if..elseif with else statements. For example:


Note: If you have multiple lines to execute, enclose each block in curly brackets.

I will explain both with visual examples in this tutorial, so you may understand how to use if..else with HTML or other web elements, as such PHP is generally used for web based projects.

A simple example to start with – the if..else

Before going to user selection based execution of a block in the if..else statement, in this example, I will simply use a static value to show how it works, to give an idea if you are a beginner.

In the example, I have simply declared a variable and assigned it a numeric value.

PHP if else

See online demo and code

Only, the if..else statement is used in the example. If the given number is 3, it will be evaluated as true in the if statement, so the statement inside the if statement will execute. If it is some other number the PHP else statement part will execute. You may copy the code and try it in your PHP editor.

The PHP elseif statement with select dropdown, div, and CSS

Following is an example to use if with elseif PHP statement. As you open the demo page, an HTML dropdown will be displayed. In the drop-down, you can select a color.

On the change event of dropdown, the same PHP file is called where I have used if with the elseif statement.

As you select a color from the dropdown, a div with the same color will be displayed.

First have a look at the demo followed by a little description:

PHP elseif

See online demo and code

In the example, following block of codes are used in the if and elseif statements of PHP:


You can see, first the chosen color is picked by using the $_REQUEST[“color”]; and assigned to the $color variable. After that, an if statement is used where the value of the color is checked. If the value is “red” it will display the div of red color by using the echo statement.

If orange was selected in the dropdown, the elseif statement after the if statement will be evaluated as true. So the div with the orange color is displayed. If some other color is chosen from the dropdown, it will keep on checking the elseif statements.

Following is the code of HTML for dropdown Where the PHP file is called with the selected color:


Note that, this example is just to demonstrate the use of if..elseif statements. In real time, you may use JavaScript to change the color of div or some other elements in web page.

A PHP if else example to check password format

In this example of the if..else statement, a user is asked to enter the email address in a textbox. After entering the email and pressing the submit button, the PHP script will check the format of email address and displays the message accordingly.

See the demo online:

PHP if else email

See online demo and code

As you submit the button, the textbox value is assigned to $address variable by using the $_POST[‘address’]. This variable is checked in the PHP if else condition to validate for the correct email format by using a built-in function, FILTER_VALIDATE_EMAIL:


If the entered email is correct, the if condition will be true and echo statement will display the given string. Otherwise, the else statement will execute the given statement.

An example of if..else with foreach loop and arrays

In this example, I will use the if statement with a foreach loop and arrays. For that, a simple numeric array is created with five elements.

The elements are: 5, 10, 15, 20, 25. After that, a foreach loop is used to display those array elements.

The task is to quit the foreach loop as it reaches the value of 15 or more.

PHP if foreach array

See online demo and code

To accomplish that, I simply used an if statement. In the if statement following condition is given:


So as soon as the value of variable $i reached 15 (which is the value of current array element), the break statement will stop the loop and control will move to the next line outside of the foreach loop.

The complete code is given in the demo page.

An example of nested if statement

In certain scenarios, you may need to check two or more conditions after the first condition is true. In that case, you should perform some action if the first condition is true and then check the second condition by using another if..else inside the first if statement.

Using another if statement inside the first if statement is nested if. See the following structure:


So inside the curly brackets of first if statement, another if statement is used before you close it. This will start nested if where you can check another condition. You may go further to use another nested if inside second if statement and so on.

After using the if statement (with else or elseif) inside the first if, close the curly bracket. There you can use elseif or else that will relate to the first if statement.


The if..else, elseif statements in PHP are used to make decisions based at some criteria. You have to give an expression in the if statement which is evaluated. If it returns as true, it will perform the given actions otherwise, it will move to the next elseif statement (if provided). If all conditions are false, the else part will execute and after that control will move to the next line of code outside of the if statement.

You may use comparison operators like ==, !=, >, <, <= etc. and logical operators like and, or, &&, || etc. to evaluate a condition.

If you have many elseif blocks then you may consider using the switch statement of PHP.