The if, elseif ...else and switch statements are used to take decision based on the different condition.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.
You can use conditional statements in your code to make your decisions. PHP supports following three decision making statements −
if...else statement − use this statement if you want to execute a set of code when a condition is true and another if the condition is not true
elseif statement − is used with the if...else statement to execute a set of code if one of the several condition is true
switch statement − is used if you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.
If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.
Syntax
The following example will output "Have a nice weekend!" if the current day is Friday, Otherwise, it will output "Have a nice day!":
See this example:
Output:
If you want to execute some code if one of the several conditions are true use the elseif statement
Syntax
The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise, it will output "Have a nice day!" −
See this example:
Output:
If you want to select one of many blocks of code to be executed, use the Switch statement.
The switch statement is used to avoid long blocks of if..elseif..else code.
Syntax
See this example:
The switch statement works in an unusual way. First it evaluates given expression then seeks a lable to match the resulting value. If a matching value is found then the code associated with the matching label will be executed or if none of the lable matches then statement will execute any specified default code.
Output: