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

Control Flow: Switch Forum

View Course » View Exercise

346 points
582f5e392524c84fad0003c2_338490890
Submitted by
Jacob Tooman
over 4 years ago

Please help me with my code... Switch Syntax

<?php
$fruit = "Apple";
 switch ($fruit) { 
    case 1 "Apple":
    echo "Yummy.";
    break;
}

What is wrong with it? "Parse error: syntax error, unexpected TCONSTANTENCAPSED_STRING on line 11"


10 votes

permalink

Here's how to add the default case.

<?php
$fruit = "Apple";

switch ($fruit) {
    case 'Apple':
        echo "Yummy.";
        break;
        default: echo "It's not a fruit";
}

?>

325 points
8fc3262e6e4d2e60cce498684d91746e?s=140&d=retro
Submitted by
MrRedd
over 4 years ago


1 vote

permalink

I think that the default option is the same thing as a a case, except you have to think about it in the IF/ELSE function. IF = case , ELSE = default.
So your coding should represent that :

$fruit = "Apple";

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

Don't forget that (break;) is important or else you'll have the code(case) appear with it.

406 points
0e42a51af5b796f9e1612ebb6ce7b25e?s=140&d=retro
Submitted by
Rony Valenzuela
over 4 years ago

4 Comments

2b153953ef015ea1708bd5f306bd2bb6?s=140&d=retro Roni over 4 years ago

You don't need break; on the very last case, in this case default :p

0e42a51af5b796f9e1612ebb6ce7b25e?s=140&d=retro Rony Valenzuela over 4 years ago

lets say you put the default case at the start.

8fc3262e6e4d2e60cce498684d91746e?s=140&d=retro MrRedd over 4 years ago

I'm not sure it will be syntactically correct if the was the case with this particular code, but it could work!

5108df1e69bf01e02d002980_626456678 Steve Cummins over 4 years ago

This explains the concept much better than the instructions on the left hand side imho. Thanks Rony.

If you read the first two lines here first, then read the instructions it all makes perfect sense.


0 votes

permalink

Remove 1 from the case. You're checking for a integer and then a string, that wouldn't work, whatever is inside the quotation marks is the condition you're checking for.

504 points
2b153953ef015ea1708bd5f306bd2bb6?s=140&d=retro
Submitted by
Roni
over 4 years ago

4 Comments

582f5e392524c84fad0003c2_338490890 Jacob Tooman over 4 years ago

Thank you for pointing out that I was "checking an integer and then a string". I understand not to mix those concepts now. However I still get a "oops, make sure you add the default case!" What does that mean? Thank you

53f3764e282ae3da53000455_587160470 Julijana Seketa over 4 years ago

I'm getting the same response Jacob Tooman is getting. I need some help. thanks

582f5e392524c84fad0003c2_338490890 Jacob Tooman over 4 years ago

I fixed it by adding "default: echo "It's not a fruit";" after the "break;" line. Thanks for all the help everyone!

Picture Jamie Young over 4 years ago

ya, I think some of these exercises are poorly written


0 votes

permalink

This works on Chromium browser.
<!DOCTYPE html>





<?php
$fruit = "Apple";
switch ($fruit) {
case 'Apple':
echo "Yummy.";
break;
default:
}
?>

1228 points
52f07fbc548c358e90000e1a_385034243
Submitted by
carlosmante
over 4 years ago


0 votes

permalink

This is my code:

$fruit = "Apple";

switch ($fruit) {
    case 1: 'Apple';
        echo "Yummy.";
        break;
    default:
        echo "It's not";
}

I don't understand the reason why the right (front end screen) gives me the default result. Shouldn't the front end agree with case 1? That fruit equals apple?

240 points
Picture
Submitted by
ComputerColumbus
about 2 years ago