Example |
<html>
|
<body>
|
<?php
|
$x=10;
|
if ($x>=10)
|
{
|
echo "Hello<br/>";
|
echo "The day is over for you.<br/>";
|
}
|
else
|
{
|
echo "Hello<br/>";
|
echo "You still have to work.<br/>";
|
}
|
?>
|
</body>
|
</html> |
| |
Output

Switch Statement
The “switch” statement is another useful conditional statement. It is used when one of the many blocks of code has to be executed. When the switch statement is used then it checks for all the conditions at once and it is also a good programming practice.
In the switch statements break is used to prevent the code from running into the next case automatically.
| Syntax |
switch (expression)
{
case label1:
code to be executed if the expression = label1;
break;
case label2:
code to be executed if the expression = label2;
break;
default:
code to be executed if the expression is different from both label1 and label2;
break;
}
|
| |
In the following example there is a single expression that is evaluated once. After this the value of the expression is compared with the value of each case in the structure. If the value in any of the cases gives a match, then the block of code associated with that structure is associated. If the value does not match with any of the cases then the default case is executed.
Example: |
|
<html> |
<body> |
|
<?php |
|
$input = 10; |
echo "Working for $input hours<br />"; |
|
switch ($input)
|
{
|
case 6:
|
echo "Your salary is $600";
|
break;
|
case 8:
|
echo "Your salary is $800";
|
break; |
case 10:
|
echo "Your salary is $1000";
|
break;
|
case 12:
|
echo "Your salary is $1200";
|
break;
|
default:
|
echo "How much do you work?";
|
break;
|
}
|
?>
|
|
</body>
|
</html>
|
|