Web Statistics

switch

I'm so fast that last night I turned off
the light switch in my hotel room and was in
bed before the room was dark. -- Muhammad Ali

switch

The switch control is basically a substitution for when the if...elseif...else control contains several elseif blocks. It's purpose in this situation is to avoid long block of code. Here is a comparison.

if...elseif...else
$d=date("D");

if ($d=="Mon")
{
    echo "Today is a Weekday";
}
elseif ($d=="Tue") {
    echo "Today is a Weekday";
}
elseif ($d=="Wed") {
    echo "Today is a Weekday";
}
elseif ($d=="Thu") {
    echo "Today is a Weekday";
}
elseif ($d=="Fri") {
    echo "Today is a Weekday";
}
elseif ($d=="Sat") {
    echo "Today is a Weekend";
}
elseif ($d=="Sun") {
    echo "Today is a Weekend";
}
else {
    echo "Must not be an Earthly day!";
}
switch...
$d=date("D");

switch ($d)
{
    case "Mon":
    case "Tue":
    case "Wed":
    case "Thu":
    case "Fri":
        echo "Today is a weekday";
        break;
    case "Sat":
    case "Sun":
        echo "Today is a weekend";
        break;
    default:
        echo "Must not be an Earthly day!";
}

Up Next

While and do...while