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

Control Flow: Switch Forum

View Course » View Exercise

707 points
530be0bc282ae32c500002e8_498715681
Submitted by
Jamal Derani
over 4 years ago

I'm really confused help!

I know this is embarrassing but I have no idea what I'm doing and my code is probably way off :'(


 <!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
    <?php
    $i = 2;
    switch (true):
        case 1:
                        echo "wrong!";
                        break;
        case 3:
                echo "wrong!";
                        break;
            case 4:
            echo "wrong!";
            break
             default:
                echo "Correct!";
     endswitch;
    ?>
    </body>
</html>

2 votes

permalink

In your example, you're trying to see if each case is equal to true. And since in programming true equals 1, it should come back saying "wrong!". Putting false, $i or any other number except 1, 3 or 4 should return "Correct!".

<?php
$i = 2;
switch (true):
    case 1:
        echo "wrong!";
        break;
    case 3:
        echo "wrong!";
        break;
    case 4:
        echo "wrong!";
        break;
    default:
        echo "Correct!";
endswitch;
?>

(Remember your semicolons!)

549 points
Picture
Submitted by
Matthew Higley
over 4 years ago

1 Comment

16549be8aab2ba368b3190967ab92f61?s=140&d=retro michaelruiter over 2 years ago

this is fail ?! i have used this !!!

Make sure you have at least three breaks! This came out


2 votes

permalink

As I was doing more research in an attempt to better my coding craft I came along a better use for the switch(true) scenario.

If you're looking for a true statement, use it in this format.

$i = 6;
switch (true):
    case ($i > 5):
        echo "The number is greater than 5";
        break;
    case ($i < 5):
        echo "The number is less than 5";
        break;
    default:
        echo "It's exactly 5";
 endswitch;

I feel better about that answer. :)

549 points
Picture
Submitted by
Matthew Higley
over 4 years ago


0 votes

permalink

A true doesn't work like that. check here for my explanation

This or Matthews code should work:

$i = 2;
switch (true):
    case ($i == 1):
        echo "wrong!";
        break;
    case ($i == 3):
        echo "wrong!";
        break;
    case ($i == 4):
        echo "wrong!";
        break;
    default:
        echo "Correct!";
endswitch;

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

5 Comments

5181b1523ac186549600141d_72134216 Bodi the surf dog over 4 years ago

why don't people actually help ?? Posting a poor solution to the problem just adds to the already frustrating aspect of:

console.log (this browser is horrible)
console.log (don't use Mozilla)
console.log (enjoy)

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

May I ask, how wasn't this any helpful? I offered him working code using the switch(true) and a link to explain how it works.

5181b1523ac186549600141d_72134216 Bodi the surf dog over 4 years ago

not you lol

51f5629252f86301b000822e_748113678 Furqan Arshad over 4 years ago

I am confused about this switch statement..

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

This is how you would make a switch(true) check for numbers, this is a terrible example. You should just use $i as the arg and make the cases on it.


-1 votes

permalink

Your switch statement "switch(true)" is the same as "switch(1)" because true == 1 so the code in "case 1:" is executed.

You want to base the result of the switch statement on the value of the variable $i

Change:
switch (true):
To:
switch ($i):

Also, you need to add a ; after your break statement in case 4:

case 4:
echo "wrong!";
break;

If you make these two changes the code does work.

69 points
Picture
Submitted by
Dan Reinders
over 4 years ago