Assignment044 - Switch

Thus far you have learned that if statements allow you to evaluate a condition and run a block of code (Based on whether a condition evaluates to true or false). In addition, you have learned that you can provide alternate blocks of code by using else statements and, if you read carefully, you can also evaluate multiple conditions by using elseif statements.

Still, even with elseif statements, your code can get rather cumbersome when evaluating many conditions. In those cases, consider using the switch statement. Use switch statements when you have many conditions to check or when conditions evaluate to non true/false values.

For example:Switch



<html>
<head>
<title>Switch</title>
</head>
<body>

<?php
//Gift certificates are given in denominations of
//50, 100, 200, 300, 400, 500, or 1000
//Based on the of the certificate you are
//going shopping and/or out with your honey.

//set the variable to 500
$giftdollars=500;

switch (
$giftdollars)
    {
        case 
50:  //case 50 and case 100 will produce the same result.
        
case 100
            print(
"I can buy shoes!");
            break;

        case 
200
            print(
"I can buy shoes and an outfit!");
            break;

        case 
300
            print(
"I can buy shoes, an outfit, and tickets to a play!");
            break;

        case 
400
            print(
"I can buy shoes, an outfit, tickets to a play, and a weekend getaway!");
            break;

        default:
            print(
"I'm buying a Playstation 3 to use as a footrest while playing my XBox 360!");
    }
?>

</body>
</html>