PHP Switch Cases

PHP Switch Cases
PHP Cases or switch case is better alternative  for If .. Else statements
Switch case is predefined systematic conditions that can execute run time.
Syntax: PHP Switch cases.


<?php



Switch(Variable Name)
{
Case 'Check 1':
echo 'print output';
break;

Case 'Check 1'
echo 'print output';
break;

default
echo 'print output';

}

?>

Description: Variable is nothing but a conditional parameter to pass a value to switch cases, these cases will check exact match with variable value and gives the result.

Example:


<?php

$day="Friday";

Switch($day)
{
Case 'Monday':
Echo ‘Todays discount is 30%’;

Case 'Friday'
Echo ‘Todays discount is 40%’;
}
Default
echo "No discount";




?>
Output: Todays discount is 40%

If when there is no marching condition found then it will execute the default statement and give the default result.

Example 2: for default execution
<?php

$day="Friday1";

Switch($day)
{
Case 'Monday':
Echo ‘Todays discount is 30%’;

Case 'Friday'
Echo ‘Todays discount is 40%’;
}
Default
echo "No discount";




?>
Output: No discount


No comments :

Post a Comment

Leave A Comment...