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

Control Flow: Switch Forum

View Course » View Exercise

327 points
E680a34f1859583e2379c467da4f8ee3?s=140&d=retro
Submitted by
avii
over 4 years ago

Example case is not correct

On line 19, it should be

echo $i.' is somewhere between 1 and 5.';

otherwise it will just print $i, instead of showing its value.

or else double quotes which evaluates will also do :

echo "$i is somewhere between 1 and 5"

0 votes

permalink

You're right in that the value of $i is not inserted into the string, but I think that is intentional: the message printed to the document is:

$i is somewhere between 1 and 5.

– the message is talking about the variable $i.

3117 points
Picture
Submitted by
Alex J
over 4 years ago

2 Comments

52565eeb80ff337232001e7d_158831513 brennanpringle_4f151ba89c99ab0001018 over 4 years ago

By any chance do you happen to know why an escape sequence is required for the $i variable when using double quotation marks, but not needed when using single quotation marks?

Picture Alex J over 4 years ago

That’s simply because single-quoted strings are printed verbatim, without interpreting anything.


0 votes

permalink

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;

1228 points
52f07fbc548c358e90000e1a_385034243
Submitted by
carlosmante
over 4 years ago