This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by penguinsk
about 10 years

[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.

Answer 52e08c269c4e9dbb7600163b

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";
}
points
Submitted by Judy
about 10 years

2 comments

penguinsk about 10 years

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

Judy about 10 years

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

Answer 52e10c259c4e9d1e0c00129d

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.

points
Submitted by elmostafa marah
about 10 years

Answer 535197e08c1ccc82a8000782

0 votes

Permalink

I still don’t understand

points
Submitted by IcyIzzy10
almost 10 years

Answer 535198de631fe99306000780

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.

points
Submitted by IcyIzzy10
almost 10 years

Answer 56025de19113cb121b0006eb

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>
points
Submitted by Anita.Guni
over 8 years