22 – PHP elseif Statement


To check for one condition, if/else statement is a powerful tool to use. However, if you check the variable $emp_name for the name of the employee which is Craig the President, Kenjie the CEO or a Manager of the company, will need an elseif statement to check 3 or more conditions.


What is elseif in php?

An elseif statement is used for three or more conditions. This elseif statement must come after the if statement because you cannot use it without having an if statement.


If…elseif…else statement once evaluated in PHP it see first if the statement is equal or  true. But, if the test results into false, it will immediately check first the elseif statement. Once proven false, the next elseif statement will be checked or it will evaluate the else segment if there are no more elseif statements, if it exists.


Using elseif with if…else

Picture out the simple problem described above, we want to find out if the employee is the President Mr. Craig. An if else statement is what we need for this example.


if Code:

However, if we also want to check if President Craig was the employee well, we have to insert an elseif clause.

elseif Code:


Good Morning Sir!

PHP checked if $emp_name was equal to “Craig“, which is evaluated to false. Then, the first elseif statement is checked. The $emp_name was indeed equal to “Craig so, it printed out the phrase “Good Morning Sir!” We could also insert more elseif statement if we wanted to check out for more of the employee names.


Looking for more source code? Type your keyword here here!


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.