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 the following chapters you will learn how to repeat code by using loops in PHP.
PHP Loops
Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
In PHP, we have the following loop types:
while
- loops through a block of code as long as the specified condition is truedo...while
- loops through a block of code once, and then repeats the loop as long as the specified condition is truefor
- loops through a block of code a specified number of timesforeach
- loops through a block of code for each element in an array
The following chapters will explain and give examples of each loop type.
All the conditional statements and loop statements are collectively termed as Control Statements. Conditional Statements: Conditional statements are used in a code to perform an action only if a particular condition is satisfied. PHP provides four types of conditional statements. These are: if: If statement is used to perform an action if a single condition is true. ..else: If else statement is used to perform an action if a single condition is true and to perform another action if that condition is false. ..elseif….else: If elseif else statement is used to perform different actions for different conditions.
switch:
Switch statement is used to select one of the action to perform from multiple actions, relative to the true condition.
Loop Statements:
Loop Statements are used to execute a block of code continuously as long as the condition of the loop is true, and stops only when the condition fails.
PHP provides three types of loop statements. These are:
For loop:
For loop is used to execute a group of action only for a specified number of times.
While Loop:
While loop is used to execute a group of action as long as the specified condition is true.
Do While Loop:
Do While loop is used to execute a block of code at least once and then to repeat the actions as long as the specified condition is true.