Statements like if, elseif,…else, and switch are used to make decisions based on different conditions.
You can decide what to do in your code with the help of \”if\” statements. PHP supports the following three statements for making decisions: −
Statements that help PHP decide what to do.
PHP statement
Use the if…else statement if you want to run one set of code when a condition is true and another set of code when the condition is not true.
The elseif statement is used with the if…else statement to run a set of code if one of several conditions is true.
If you want to choose which of several blocks of code to run, you can use the Switch statement. The switch statement is used to get rid of long blocks of if..elseif..else code.
If…else Statement
Use the if…else statement to run one set of code if a condition is true and another set of code if the condition is false.
Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Example
The output of the following code will be \”Have a great weekend!\” if today is Saturday, it will say \”Have a great day!\”; otherwise, it will say \”Have a nice day!\”:
<html>
<body><?php
$d = date(\”D\”);if ($d == \”Sat\”)
echo \”Have a great weekend!\”;else
echo \”Have a great day!\”;
?></body>
</html>
Output −
Have a nice weekend!
The ElseIf Condition
Use the elseif statement if you want to run some code only if one of several conditions is met.
Syntax
if (condition)
code execute if condition is true;
elseif (condition)
code execute if condition is true;
else
code execute if condition is false;
Example
If the current day is Saturday, the following code will say \”Have a great weekend!\” and if the current day is Sunday, it will say \”Enjoy your Sunday!\” If not, it will say \”Have a good day!\”
<html>
<body><?php
$dat = date(\”D\”);if ($dat == \”Sat\”)
echo \”Have a great weekend!\”;elseif ($dat == \”Sun\”)
echo \”Enjoy your Sunday!\”;else
echo \”Have a good day!\”;
?></body>
</html>
Output −
Have a nice Weekend!
Switch Statement
Switch statement is used to choose which of several blocks of code to run.
With the switch statement, you can avoid long blocks of if..elseif..else code.
Syntax
switch (expression){
case condition1:
code execute if expression = condition1;
break;case condition2:
code to be executed if expression = condition2;
break;default:
code to be executed
if expression is differentfrom both condition1 and condition2;
}
Example
The way the switch statement works is strange. First, it looks for a label that matches the value of the expression. If a matching value is found, the code for the matching label will be run. If no matching label is found, the then statement will run any default code that was set.
<html>
<body><?php
$dat = date(\”D\”);switch ($dat){
case \”Mon\”:
echo \”Today is Monday\”;
break;case \”Tue\”:
echo \”Today is Tuesday\”;
break;case \”Wed\”:
echo \”Today is Wednesday\”;
break;case \”Thu\”:
echo \”Today is Thursday\”;
break;case \”Fri\”:
echo \”Today is Friday\”;
break;case \”Sat\”:
echo \”Today is Saturday\”;
break;case \”Sun\”:
echo \”Today is Sunday\”;
break;default:
echo \”Wonder day is this\”;
}
?></body>
</html>
Output −
Today is Saturday