From the PHP manual:
In PHP, you can also write 'else if' [in two words] and the behavior would be identical to the one of 'elseif' [in a single word]. The syntactic meaning is slightly different [if you're familiar with C, this is the same behavior] but the bottom line is that both would result in exactly the same behavior.
Essentially,
they will behave the same, but else if
is technically equivalent to a nested structure like so:
if [first_condition]
{
}
else
{
if [second_condition]
{
}
}
The manual also notes:
Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.
Which means that in the normal control structure form [ie. using braces]:
if [first_condition]
{
}
elseif [second_condition]
{
}
either elseif
or else if
can be used. However, if you use the alternate syntax, you must use elseif
:
if [first_condition]:
// ...
elseif [second_condition]:
// ...
endif;
Conditional statements are used to perform different actions based on different conditions.
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
if
statement - executes some code if one condition is trueif...else
statement - executes some code if a condition is true and another code if that condition is falseif...elseif...else
statement - executes different codes for more than two conditionsswitch
statement - selects one of many blocks of code to be executed
PHP - The if Statement
The if
statement executes some code if one condition is true.
Syntax
if [condition] {
code to be executed if condition is true;
}
Example
Output "Have a good day!" if the current time [HOUR] is less than 20:
Try it Yourself »
PHP - The if...else Statement
The if...else
statement executes some code if a condition is true and another code if that condition is false.
Syntax
if [condition] {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Example
Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
Try it Yourself »
PHP - The if...elseif...else Statement
The if...elseif...else
statement executes different codes for more than two conditions.
Syntax
if [condition] {
code to be executed if this condition is true;
} elseif [condition] {
code to be executed if first condition is false and this condition is true;
}
else {
code to be executed if all conditions are false;
}
Example
Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":
Try it Yourself »
PHP - The switch Statement
The switch
statement will be explained in the next chapter.
PHP Exercises
In this guide, we will be showing you how to use the if, else, and elseif conditional statements in PHP.
Conditional statements are one of the first things you should learn when you are starting out with the PHP language.
These statements allow you to perform an action when your stated condition is met. They are a critical part of writing a script in PHP.
Over the next few sections, we will touch on three of these conditional statements. We will be touching on the “if
“, “if...else
“, “if...elseif
“, “if...elseif...else
” conditional statements as well as nested if statements.
Code contained within both the if
and else if
statements will only run
if the statement returns true
.
Table of Contents
- The if Statement
- Example of Using the if Statement
- The if…else Statement
- Example of Using an if…else Statement
- The if…elseif Statement
- Example of Using the if…elseif Statement
- The if…elseif…else Statement
- Example of Using the if…elseif…else Statement
- Nested if Statements
- Example of Using a Nested if Statement
- Conclusion
The if Statement in PHP
The first PHP conditional statement that we will be touching on is the if
statement. It is the most simple statement you can use in the PHP language.
The
if statement allows you to check if a particular condition evaluates to true. If the statement is true, PHP will run the code within the curly brackets [{}
].
Below is an example of how you would write a simple if statement within your PHP script.
Example of Using the if Statement
We will now run you through a simple PHP script we wrote to show you how the if statement works.
Within this example, we will get the current
day using the date[]
function and store it in a variable called “$current_day
“.
Using the if
statement within our PHP script, we will check to see if the current day equals “Mon
“.
If our day is Monday, then our script will use the echo statement to print out the text “The day is Monday
“.
The if…else Statement in PHP
The next PHP conditional statement we
will be looking at is the else statement. You must always use an else statement last in your condition chain. For example, after an “if
” or “else if
” statement.
The else
statement allows you to perform a task if your if
condition returned false
.
This conditional statement is useful when you want code to be executed if your if
statement condition isn’t met.
Below is an example of a typical if...else
statement when written in a PHP script.
Example of Using an if…else Statement
Now let us go into a simple example of using the if...else
statement. We have expanded upon our if statement example.
If our “$current_day
” variable is equal to “Mon
“, then our script will print out the text “The day is Monday
“.
If the “$current_day
” variable is not equal
to “Mon
“, PHP will run the code within our else
statement.
Within this else statement, we use echo to display the text “The day is not Monday
” to the output.
The if…elseif Statement in PHP
The PHP language allows you to add a conditional statement after an if
statement. This is what is called the “elseif
” statement. You can also write the “elseif
” statement as “else if
“.
They are used after an if
statement. It
allows you to check if a conditional statement returns true if your previous statement returns false
.
This is useful if you want to check another condition if the first one proved to be false.
Below is an example of writing an if...elseif
statement within a PHP script.
Example of Using the if…elseif Statement
Let us run through the following example. This example will show you how a if...elseif
statement can be written
within a PHP script.
With this example, we retrieve the current day of the week and store using the “date[]
” function and store it into our “$current_day
” variable.
We then have our first if statement. This conditional statement checks if our current day equals “Mon
“.
If the day is not equal to “Mon
“, it falls through to our elseif
statement.
Within this elseif
statement, we check if the current day is equal to “Tue
“, we will echo the text “The day is Tuesday
“.
The if…elseif…else Statement in PHP
The if...elseif...else
statement shows how you can combine three conditional statements in PHP.
You always need to start a conditional statement by using the if statement.
After this, you can now use either an “else
” or “elseif
” statement. As we want to check for an additional condition, we will be utilizing the “elseif
” statement.
You can add as many elseif
statements as
you would like. So you can use them one after another without issue. However, it is better to use a switch statement in some cases.
Finally, this chain is ended with an else
statement. If the first if
statements weren’t met, then PHP will run the code within this statement.
Below is an example of how you would write the “if...elseif...else
” statement within a PHP script.
Example of Using the if…elseif…else Statement
With this example, we retrieve the current day using the date function and store it within the “$current_day
” variable.
With our first if
statement in this PHP script, we check if the current day is set to “Mon
“. The text “The day is Monday
” will be displayed if it currently Monday.
If the day isn’t Monday, it will fall through to our second conditional statement. With the “elseif
” statement, we check if the “$current_day
” variable is set to “Tue
“. If the current day is Tuesday,
we use echo to print the text “The Day is Tuesday
“.
If the previous two conditional statements return false, the code will fall into the else
statement.
Within this else statement, we use the PHP echo statement to print out the text “The day is not Monday or Tuesday
“.
Another useful way to use if statements within PHP is to nest them within each other. Using an if statement in this way, you can check for a condition if the first statement returns true.
A nested statement allows you to run a second conditional statement if the first statement returns true.
Below we have included an example of writing a nested if statement within PHP.
Example of Using a Nested if Statement
We will show you a basic usage of nested if statements for this PHP example.
Like our previous examples, we start by storing the current day within our "$current_day
” variable.
We then utilize the if
statement to check if the
“$current_day
” variable equals “Mon
.”. If the condition evaluates to true, PHP will run the code block within that statement.
The first thing we do within our if statement is echo the text “The day is Monday
“. We then create a new variable called “$current_hour
” where we utilize the date[]
function to retrieve the current hour in 24-hour format.
We then get to our nested if statement. For the condition for this statement, we check to see if the current hour is greater than
[>
] or equal to [=
] the number 18
[6:00 PM in 24 hour time].
If our nested condition matches we use echo to show the text “It is night time
“.
Conclusion
Within this guide, you will have learned how to use various forms of the if conditional statement within the PHP programming language. These forms include the if
, if..else
, if...elseif
, and if...elseif...else
statements.
Conditional statements are a core part of the PHP language as they help you perform different actions depending on the results of your conditions.
Additionally, we also touched on how you can use nested if statements.
If you have any questions about using if
statements within the PHP language, please comment below.
Be sure to check out our other PHP tutorials or if you are interested in other programming languages, check out our general coding guides.
Weekly Updates Straight To Your Inbox
Receive our Raspberry Pi projects, coding tutorials, Linux guides and more!