This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Control Flow: Switch Forum

View Course » View Exercise

1537 points
22988510868a95f108578e96f6fd2833?s=140&d=retro
Submitted by
penguinsk
over 3 years ago

[resolved] 3/6 What is a default case?

<?php
$fruit = "Apple";

switch ($fruit) {
    case 'Apple':
        echo "Yummy.";
        break;
}

?>

that is my code and the error says :
Oops, try again. Make sure you add the default case!

but it returns Yummy.

also- im not really understanding what the default case really mean so if you could explain that it would be great.


5 votes

permalink

You can put in a default case that will handle anything that didn't match any of the specific cases that you coded for.

Here is an example of a switch with a default case:

switch (2) {
    case 0:
        echo 'The value is 0';
        break;
    case 1:
        echo 'The value is 1';
        break;
    case 2:
        echo 'The value is 2';
        break;
    default:
        echo "The value isn't 0, 1 or 2";
}

3692 points
55df2afa95e3787b9200005e_359368177
Submitted by
Judy
over 3 years ago

2 Comments

22988510868a95f108578e96f6fd2833?s=140&d=retro penguinsk over 3 years ago

thanks, shoulda known as it was called DEFAULT CASE haha.

55df2afa95e3787b9200005e_359368177 Judy over 3 years ago

Glad I could help. When you look back in a few months it will all seem so much more sensible :)


0 votes

permalink

In the editor you have the code of the previous exercise. Make the code use the alternative syntax rather than the curly-brace syntax.
<!DOCTYPE html>





<?php
$i = 5;

switch ($i) {
    case 0:
        echo '$i is 0.';
 break;

    case 1:
    case 2:
    case 3:
    case 4:
    case 5:

        echo ' 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.";


}
?>
</body>

254 points
52c8a879548c353521003979_382663031
Submitted by
elmostafa marah
over 3 years ago


0 votes

permalink

I still don't understand

181 points
53504d048c1cccf572000158_472665248
Submitted by
IcyIzzy10
over 3 years ago


0 votes

permalink

never mind! I figured it out! Its really easy! You just have to use the example the creator of the PHP language gives you.

181 points
53504d048c1cccf572000158_472665248
Submitted by
IcyIzzy10
over 3 years ago


0 votes

permalink

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
    <?php
    $fruit = "Apple";

    switch ($fruit) {
       case:
           echo "Apple";
            echo "Yummy.";
            break;
            default:
    }

    ?>
    </body>
</html>

618 points
22686a57dafc98d3ac407c56564b90b1?s=140&d=retro
Submitted by
Anita.Guni
almost 2 years ago