PHP Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax:

switch(n)
{
            case1:
statement if n=case1;
break;
            case2:
            statement if n=case2;
break;
            case3:
            statement if n=case3;
break;
              ...
default:
statement if n is different from all labels;
}
Working: First you have a single expression “n” (most frequently a variable), that is evaluated once. The value of the expression is compared with the values for each case in the structure. If any match found, the block of code associated with will be executed. Use break to prevent the code from executing into the next case automatically. The default statements is used if no match is found. PHP Switch Statement Flow Chart Example:
<?php
$num=30;   
switch($num){   
case 10:   
echo("number is equals to 10");   
break;   
case 20:   
echo("number is equal to 20");   
break;   
case 30:   
echo("number is equal to 30");   
break;   
default:   
echo("number is not equal to 10, 20 or 30");   
}  
?>
Output:                
number is equal to 30
Example2:
<?php
$today = date("D");
switch($today){
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 "Not Found.";
break;
}
?>
Output:
Today is Thursday.
Example3:
<?php
$num = 1;
switch ($num)
 {
case 1: echo 'The number is 1!';
break;
case 2: echo 'The number is 2';
break;
default: echo 'Unknown number';
}
?>
Output:
The number is 1!
Operator Description Example
Less than ($age < 18)
Greater than ($age > 18)
<= Less than or equal to ($age <= 18)
>= Greater than or equal to ($age >= 18)
!= Not equal to ($age != 18)
== Equal to ($age == 18)
Example4:
<?php
$age = 21;
switch ($age)
{
case 17: echo 'You\'re not old enough to play GTA!';
break;
case 17: echo 'You\'re old enough to drive!';
break;
case 18: echo 'You\'re old enough to play GTA!';
break;
case ($age > 18): echo 'You\'re quite old!';
break;
default: echo 'Was that a number?';
}
?>
Output:
You're quite old!
Example5:
<?php
extract($_POST);
if(isset($save))
{
switch($ch)
{
case 'ADD(+)':
$res=$fn+$sn;
break;
case 'SUB(-)':
$res=$fn-$sn;
break;
case 'MUL(*)':
$res=$fn*$sn;
break;
case 'DIV(/)':
$res=$fn*$sn;
}
}
?>
<form method="post">
<table align="center">
<tr>
<th> Enter First Number</th>
<th><input type="number" name="fn" value="<?php  echo @$fn;?>"/></th>
</tr>
<tr>
<th> Enter Second Number</th>
<th><input type="number" name="sn" value="<?php  echo @$sn;?>"/></th>
</tr>
<tr>
<th>Select Your Choice</th>
<th>
<select name="ch">
<option>ADD(+)</option>
<option>SUB(-)</option>
<option>MUL(*)</option>
<option>DIV(/)</option>
</select>
</th>
</tr>
<tr>
<th>Your Result</th>
<th><input type="number" readonly="readonly" disabled="disabled"
value="<?php  echo @$res;?>"/></th>
</tr>
<tr>
<thcolspan="2">
<input type="submit" name="save" value="Show Result"/>
</th>
</tr>
</table>
</form>
Output: PHP Switch Statement example



ADVERTISEMENT
ADVERTISEMENT