0

Why my code not run when i put -1 or 4+? what is the wrong? anyone help me?

<?php //LIFT of my 4 storyed Building... :) $select_the_floor=0; switch ($select_the_floor) { case '1': echo "welcome to Level 1"; break; case '2': echo "welcome to level 2"; break; case '3': echo "welcome to level 3"; break; case '4': echo "welcome to level 4"; break; if ($select_the_floor<1) { echo "Ground floor is not available right now and "; } elseif ($select_the_floor>4) { echo "This is 4 storyed Building and More level is coming soon"; } } ?>

14th May 2019, 8:03 AM
🇧🇩BASHAR
🇧🇩BASHAR - avatar
5 ответов
+ 4
You're welcome, I'm glad if it helps my friend 👍
14th May 2019, 3:33 PM
Ipang
+ 3
You can't do if statements inside a switch statement. I would rework your code to be: if ( < 1) { Thing } elseif ( > 4) { Other thing } else { switch() { More things } }
14th May 2019, 8:07 AM
Adam
Adam - avatar
+ 3
You are placing the `if..else if` block outside any valid case. You can actually use `if..else` block inside any of the case (even inside the `default` case), just make sure they go before the `break` statement. Here I revised your code, moved the `if..else` block into the `default` case. <?php $select_the_floor = 5; switch ( $select_the_floor ) { case 1: case 2: case 3: case 4: echo "Welcome to Level $select_the_floor"; break; default: if ($select_the_floor < 1) { echo "Ground floor is not available right now."; } elseif ($select_the_floor > 4) { echo "This is 4 storyed Building and More level is coming soon."; } break; } ?> Hth, cmiiw
14th May 2019, 8:43 AM
Ipang
+ 2
Code is unreachable, due placing it after the break; statememt in your switch block. Insert a default: to handle any other case.
14th May 2019, 9:22 AM
Daniel Adam
Daniel Adam - avatar
+ 1
Ipang thank you Sir, you solved the problem easily...
14th May 2019, 10:06 AM
🇧🇩BASHAR
🇧🇩BASHAR - avatar