Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing [an empty statement]. Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The various statement types are described in this chapter.
See Also
The following are also considered language constructs even though they are referenced under functions in the manual.
- list[]
- array[]
- echo
- eval[]
There are no user contributed notes for this page.
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
When the program has conditions then statements are used.
the condition means when we want to do something when something will happen, for example, there is a situation where we want to add number when number if even.'
to apply these kinds of situations in a program conditional statements are used.
There are the following types of statements.
- if statement
- if-else statement
- if-else if statement
- nested if statement
- switch statement
- conditional operator statement
if statement
when the program has only one condition and related statement then if statement is used.
if-else statement
when the program, has only one condition and related two statement then the if-else statement is used.
if-else-if statement
when the program has multiple conditions and related statements if-else-if statement is used.
Nested if statement
when the program has a condition within other condition, there we use nested if statement.
Switch Statement
It is an alternative statement of if-else if statement there is little difference is that relational, logical operators can not be used here.
Conditional operators
It is an alternative to the if-else statement, and it is an inline conditional statement.