Switch Syntax - 'Yummy'


#1

<?php
$fruit = "Apple";

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

?>

Although in case 1 it says 'Apple', it still displays 'Not Yummy' when i submit the code. Can someone tell me, where my mistake is? Thanks in advance! :smiley:


#2

Ok, I have the answer now :smile:

<?php
$fruit = "Apple";

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

?>

#3

You removed the other echo too though >.<
so now it will NEVER say Not yummy.


#5

The reason it does not display properly is because case are always starts from (0) not 1!

    switch ($fruit) {
        case(0): "Apple";
            echo "Yummy.";
            break;
            
        default: 
            echo "That's not even the fruit!";
    }

#6

Yes, It worked... Thanks


#7

cutting the code from the and then resetting then pasting it back usually works too