Control Flow: Switch 5/6

Using "Endswitch". Syntactic Sugar!
You have two ways of creating a switch. First, there's the way we have made all the past exercises:
switch ($i) { 

}
But we can also make it this way:
switch ($i):

endswitch;
This is called alternative syntax. It exists to provide syntactic sugar
There's no difference when using either thecurly brace syntax (first example) oralternative syntax (second example), it only provides readability, thus it's usually used when mixing HTML and PHP code in the same file.
Instructions
In the editor you have the code of the previous exercise. Make the code use thealternative syntax rather than the curly-brace syntax.
?
Hint
Test how well you're learning and fill in gaps with Codecademy Pro.Learn more.
Compare the first and second example. You replace the first curly brace with a :(colon) and the last curly brace withendswitch;
Control Flow: Switch 5/6
<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
    <?php
    $i = 5;

    switch ($i) :
        case 0:
            echo '$i is 0.';
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            echo '$i is somewhere between 1 and 5.';
            break;
        case 6:
        case 7:
            echo '$i is either 6 or 7.';
            break;
        default:
            echo "I don't know how much \$i is.";
            break;
    endswitch;
    ?>
    </body>
</html>

Comments

Popular posts from this blog

MEGA 暫存檔案刪除

IOS GCD多執行緒

利用CMD指令強制刪除