PHP If Else Elseif Statements

In this article, we’ll look at PHP if else Elseif statements. We will also examine how it works, and how to use it to control execution flow.

Here are some examples to demonstrate how the statement can be used in practice, as well as its syntax and usage.

A PHP if else Elseif statement is a very useful and effective method of PHP programming.

It allows you to test multiple conditions at once and execute different sets of code according to those conditions.

This includes a block of code that is based on those conditions. It is used when multiple conditions need to be checked, and different actions need to be taken based on the results.



Conditional Statements in PHP

In conditional statements, different actions are performed depending on the conditions. In PHP, you can use the if/else, switch, and ternary operators to control code execution.

PHP has the following conditional statements:

  1. PHP if statement – runs code if a condition is met.
  2. PHP if else statement – executes one code if a condition is true and another code when it is false.
  3. PHP if elseif else statement – executes different codes for multiple conditions.
  4. PHP switch statement – executes one block of code out of many.

PHP if Statement

In PHP if statement, different actions can be performed based on different conditions.

When an if statement is used in an expression, it evaluates a particular condition and executes a block of code only if the condition is true.

Syntax

if (condition) {
A true condition will trigger the execution of the code;
}

In the below example the output will be “Have a good day!” if the current hour is less than 16:

Example: 

<?php $time = date("H"); if ($time < "16") { echo "Have a good day!"; } ?>

PHP If Else

Here is an example to print “You are an adult!” if the age is more than 18:

Example: 

<?php $age = 20 if ($age > 18) { echo "You are an adult!"; } ?>

PHP If Else Statement

PHP If Else statement is used to execute code blocks based on the evaluation of a condition.

you can perform different actions depending on the conditions.

PHP If Else statement first evaluates the condition, which can be any expression that returns a boolean value (true or false).

If the condition is true, the code block within the first set of curly braces { } is executed.

If the condition is false, the code block within the second set of curly braces is executed.

Syntax

if (condition) {
If condition is true, execute code;}
else {
If condition is false, execute code;
}

In the below example the output will be “Have a good day!” if the current hour is less than 16, otherwise the output will be “Have a sweet night!” :

PHP If Else Example: 1 

<?php $time = date("H"); if ($time < "16") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>

The following example prints “You are an adult!” if your age is greater than 18. Otherwise, it will print “You are young!”:

PHP If Else Example: 2 

<?php $age = 18; if ($age > 18 ) { echo "You are an adult!"; } else { echo "You are young!"; } ?>

PHP If Else statement output


PHP If Elseif Else Statement

Using the if-elseif-else statement, you can test multiple conditions and determine what actions to take based on the results.

It is an extension of the standard if-else statement, which allows you to use else if statements if you want to test multiple conditions within that statement.

Syntax

if (condition) {
When this condition True, execute the code;}
elseif (condition) {
When this condition True and first condition is False, execute the code;
} else {
When all above conditions are False, execute the code;
}

In the below example the output will be “Have a good morning!” if the current hour is less than 10, and “Have a good day!” if the current hour is less than 16, otherwise the output will be “Have a sweet night!” :

Example: 

<?php $time = date("H"); if ($time < "10") { echo "Have a good morning!"; } elseif ($time < "16") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>

In the example below it will print “You are an adult!” if your age is more than 18, and “You are too old!” if your age is greater than or equal to 60, otherwise it will print “You are young!”:

Example: 

<?php $age = 18; if ($age > 18 && $age < 60 ) { echo "You are an adult!"; } elseif ($age >= 60) { echo "You are too old!"; } else{ echo "You are young!"; } ?>

Example Explanation

In above example we define a variable $age and assigns it a value of 18.

It then uses an if statement to check whether the age is higher than 18 AND less than 60.

If both conditions are true, the code will execute the first statement and display “You are an adult!”.

If the age is 60 or above, the code will instead execute the elseif block and display “You are too old!”.

If the age is less than 18, the code will skip the first two blocks and execute the else block, displaying “You are young!”.

So in this example, since the value of $age is 18, the first condition is true, and the output will be “You are an adult!”.


PHP Switch Statement

The switch statement in PHP is another conditional method that allows you to execute different blocks of code according to the value of an expression in the conditional statement.

In addition to the PHP if else elseif statement, a switch statement is a useful alternative when comparing multiple values against a single expression that can be compared against a set of values.

Switch statements can be used to select one of several blocks of code that should be run by executing the statement.

Syntax

switch (variable)
{
case label1:
This code will be executed if variable=label1 is true;
break;
case label2:
This code will be executed if variable=label2 is true;
break;
case label3:
This code will be executed if variable=label3 is true;
break;
…
default:
Default will be executed if variable is different from all labels;
}

In order to implement the switch cases, we need to start with one expression var (usually a variable) that is evaluated once.

A comparison is then made between the value of the expression and each of the values for each instance in the structure.

This means that when there is a match, the block of code associated with that case will be executed.

You can use break to prevent the code from running into the next case automatically.

Default statements are used when there is no match between the two statements.

Let’s create a switch statement for flowers and find the “rose” in the switch cases:

Example: 

<?php $flower = "rose"; switch ($flower) { case "tulip": echo "This flower is tulip!"; break; case "rose": echo "This flower is a rose!"; break; case "sunflower": echo "This flower is a sunflower!"; break; default: echo "No flower are chosen"; } ?>

Here is another example of creating a switch statement for a weekday search for “wed” in all cases:

Example: 

<?php $day = "wed"; switch ($day) { case "mon": echo "Today is monday!"; break; case "tues": echo "Today is tuesday!"; break; case "wed": echo "Today is wednesday!"; break; case "thurs": echo "Today is thursday!"; break; case "fri": echo "Today is friday!"; break; case "sat": echo "Today is saturday!"; break; default: echo "Today is sunday"; } ?>

Example Explanation:

Above example demonstrates the use of a switch statement in PHP to display a message based on the value of a variable.

The code defines a variable $day and assigns it the value “wed”.

The code then uses a switch statement to evaluate the value of $day.

The switch statement checks the value of $day against a series of case statements, each representing a possible value of $day. If the value of $day matches one of the case statements, the code inside that case is executed.

If the value of $day does not match any of the case statements, the code inside the default block is executed.

In this particular example, since the value of $day is “wed”, the code will execute the third case statement, which displays the message “Today is Wednesday!” using the echo statement.

The break statement is used to exit the switch statement after executing the matching case block.

So the code demonstrates how to use a switch statement in PHP to execute different blocks of code depending on the value of a variable. It also shows how the break statement is used to exit the switch statement after executing a matching case block.

Conclusion

PHP If Else is a powerful statement for controlling the flow of your code based on certain conditions.

By evaluating a condition and executing a code block based on the result, you can create more dynamic and responsive programs.

It is important to understand the syntax of PHP If Else statements and how to structure your code properly in order to make the most of this feature in PHP.

With practice and experimentation, you can use PHP If Else statements to create more efficient and effective code.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *