This forum is now read-only. Please use our new forums! Go to forums
banner
Close banner
Why i can't run it?
Answer 515afb653552daad540012de
9 votes
you have a too high error in your syntax, see my code of example! below:
<?php
$i = 5;
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;
case 6:
case 7:
echo '$1 is either 6 or 7.';
break;
default:
echo "I don't know how much \$i is";
break;
endswitch;
?>
Answer 515c4366747ca6a6ab00008b
0 votes
It doesnt work because $i == 5
. And you don’t have case 5:
in your code.
You only have case 2
and then your code skips to the situation when $i == 6
And what are you trying to acomplish with that code? Translated into english it would be like:
$i is 2
if $i is 2 chek if $i is either 3, 4, 5 and, if it isn't, do nothing!
It is obvious that, as long as $i == 2, it can’t simoultaneusly be 3, 4 or 5.
Answer 5205bbb6abf82173b5001b4d
0 votes
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;
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
3 comments
thx!
I know this is right ,but i want to use the code that named “falling through”
In the code in this answer case 1, case 2, case 3, and case 4 all “Fall Through” to the code in case 5. At the end of the code in case 5 the break statement ends the execution of the switch statement for those cases.
case 6 falls through to case 7 and then the break ends the execution of the switch statement for those cases.