Wednesday 18 December 2013

Switch in PHP examples

Example No.1
<?php
$week = "Friday";
switch($week)
{
    case "Sunday":
    echo "Today is Sunday";
    break;
    case "Monday":
    echo "Today is Monday";
    break;
    case "Tuesday":
    echo "Today is Tuesday";
    break;
    case "Wednesday":
    echo "Today is Wednesday";
    break;
    case "Thursday":
    echo "Today is Thursday";
    break;
    case "Friday":
    echo "Today is Friday";
    break;
    case "Saturday":
    echo "Today is Saturday";
    break;
 }

?>
output:
Today is Friday

Example No.2
using default in switch
<?php
$week = "Notavailble";
switch($week)
{
    case "Sunday":
    echo "Today is Sunday";
    break;
    case "Monday":
    echo "Today is Monday";
    break;
    case "Tuesday":
    echo "Today is Tuesday";
    break;
    case "Wednesday":
    echo "Today is Wednesday";
    break;
    case "Thursday":
    echo "Today is Thursday";
    break;
    case "Friday":
    echo "Today is Friday";
    break;
    case "Saturday":
    echo "Today is Saturday";
    break;
    default:
    echo "This weekend is not availabe in our list";
}
?>
output:
This weekend is not availabe in our list

Example NO.3
Always used break keyword in the end of case body.Otherwise you got wrong output.Following example shows What you got when you did not used break keyword

<?php
$week = "Sunday";
switch($week)
{
    case "Sunday":
    echo "Today is Sunday";
   
    case "Monday":
    echo "Today is Monday";
   
    case "Tuesday":
    echo "Today is Tuesday";
   
    case "Wednesday":
    echo "Today is Wednesday";
   
    case "Thursday":
    echo "Today is Thursday";
   
    case "Friday":
    echo "Today is Friday";
   
    case "Saturday":
    echo "Today is Saturday";
   
    default:
    echo "This weekend is not availbe in our list";
}


?>
output:
Today is SundayToday is MondayToday is TuesdayToday is WednesdayToday is ThursdayToday is FridayToday is SaturdayThis weekend is not availbe in our list




No comments:

Post a Comment