There is a way to make your script adjust to different situations. A level of diversity can be added with conditional statements. Using PHP if else, elseif and if statements, you can make your code behave in different ways according to certain conditions.
PHP if else, elseif and if statements are especially useful (and therefore commonly used) when creating interactive web applications. You can make them understand different conditions, then react and act accordingly. However, each of these conditional statements works a bit differently than the others.
Contents
PHP if else and elseif: Main Tips
- PHP
if else
,elseif
andif
are statements used to declare conditions under which certain code must be executed. - They are extremely useful if you want to make your scripts dynamic, i.e.: making them react to a form submission or a certain value being applied to a specific variable.
- Conditional statements work by checking whether the conditions we specified are true or false.
if Explained
The PHP if
statement executes a certain code if conditions specified next to it are true. The code we have in the example below should display the text Good day, bud! if the local time is earlier than 20:00 (8 PM):
<?php
$time = date("H");
if ($time < "20") {
echo "Good day, bud!";
}
?>
Definition of if else
if else
PHP statements execute certain code if the condition specified next to the if
part proves true. If it is not, the code next to the else
statement is executed as an alternative.
Let's see if else PHP statements in action to get a better idea. The code you can see in the example is meant to output the text Good day, bud! if the current time is earlier than 20:00 (8 PM). If the condition isn't met (it's after 20:00), Good night, pal! will be shown instead:
<?php
$time = date("H");
if ($time < "20") {
echo "Good day, bud!";
} else {
echo "Good night, pal!";
}
?>
Overall Usage of Conditional Statements
As we have already mentioned, these statements will execute a certain code if the condition specified next to the if
part is true.
However, if it is not met, the script goes through the following PHP elseif
statement and executes the code next to the specific PHP elseif
statement if its condition is met.
Ultimately, if none of the conditions listed are met, the script then executes the code assigned to the else
statement.
Let's see an example to understand it better. The code we have here will display different text according to the current time:
- Good morning, chum! - if it is earlier than 10 AM.
- Good day, bud! - if the previous condition wasn't met and it's less than 20:00 (8 PM).
- Good night, pal! - if none of the specified conditions are met.
<?php
$time = date("H");
if ($time < "10") {
echo "Good morning, chum!";
} elseif ($time < "20") {
echo "Good day, bud!";
} else {
echo "Good night, pal!";
}
?>
Note: When using current time as a condition, don't forget to pay attention to the timezones!
PHP if else and elseif: Summary
- Using
if else
,elseif
andif
statements lets you set certain conditions in which a specific block of code will run. - Conditional statements detect if the defined conditions are true or false, and then act accordingly.
- This provides your script with flexibility and turns it more dynamic.