mastubbs Posted May 24, 2013 Share Posted May 24, 2013 (edited) Ok so i have a few rather odd problems with switch case. I have no clue why these things are happening: PAR IS: <?php $par = isset($_GET['par'])?$_GET['par'] : ''; switch ($par) { case $par >=0 && $par <=2: echo 'par 0-2'; echo '<br/> PAR: '.$par; break; case $par >=3 && $par <=5: echo 'par 3-5'; echo '<br/> PAR: '.$par; break; case $par >=6: echo 'par >=6'; echo '<br/> PAR: '.$par; break; default: echo 'ERROR: invalid PAR score passed. Please contact server administrator'; } ?> if par >=1 the script works no problems BUT 1) if par is 0 echos 'par 3-5 PAR: 0' rather than 'par 0-2 PAR: 0' this seems very odd as 1 and 2 work fine! 2) if there is no value for par, it returns 'par 3-5 PAR: ', not default message These problems seem very odd to me. does anyone have a clue why this is happening?? Thanks in advance for any replies! Edited May 24, 2013 by mastubbs Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/ Share on other sites More sharing options...
Jessica Posted May 24, 2013 Share Posted May 24, 2013 $par = isset($_GET['par'])?$_GET['par'] : ''; switch ($par) { case 0: case 1: case 2: echo 'par 0-2'; echo '<br/> PAR: '.$par; break; case 3: case 4: case 5: echo 'par 3-5'; echo '<br/> PAR: '.$par; break; default: echo 'par >=6'; echo '<br/> PAR: '.$par; }What do you get for that? Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432112 Share on other sites More sharing options...
kicken Posted May 24, 2013 Share Posted May 24, 2013 Switch statements are for comparing a single value, to another single value. Not for comparing ranges. If you want to do ranges you should just chain together some IF statements. if ($par >= 0 && $par <= 2){ .. } else if ($par >= 3 && $par <= 5){ ... } else if ($par >= 6){ ... } Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432114 Share on other sites More sharing options...
Psycho Posted May 24, 2013 Share Posted May 24, 2013 I have to disagree with Kicken. The problem with the original code was that the value in the switch is compared to the value in the case statements. So, if you have switch($par) and case $par >=0 && $par <=2: It is comparing if $par is equal to ($par >=0 && $par <=2) - which would be either true/false. What you want to do is make the value for the switch be TRUE. Then create whatever comparisons you want for the case statements. This will work <?php $par = isset($_GET['par']) ? $_GET['par'] : ''; switch (true) { case ($par >= 0 && $par <= 2): echo 'par 0-2'; echo '<br/> PAR: '.$par; break; case ($par >= 3 && $par <= 5): echo 'par 3-5'; echo '<br/> PAR: '.$par; break; case ($par >= 6): echo 'par >=6'; echo '<br/> PAR: '.$par; break; default: echo 'ERROR: invalid PAR score passed. Please contact server administrator'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432115 Share on other sites More sharing options...
Jessica Posted May 24, 2013 Share Posted May 24, 2013 I was trying to find your previous post about that Psycho but couldn't, glad you posted it again Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432120 Share on other sites More sharing options...
Q695 Posted May 25, 2013 Share Posted May 25, 2013 I asked the exact same question about 2 weeks ago. Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432165 Share on other sites More sharing options...
mastubbs Posted May 25, 2013 Author Share Posted May 25, 2013 I have to disagree with Kicken. The problem with the original code was that the value in the switch is compared to the value in the case statements. So, if you have switch($par) and case $par >=0 && $par <=2: It is comparing if $par is equal to ($par >=0 && $par <=2) - which would be either true/false. What you want to do is make the value for the switch be TRUE. Then create whatever comparisons you want for the case statements. This will work <?php $par = isset($_GET['par']) ? $_GET['par'] : ''; switch (true) { case ($par >= 0 && $par <= 2): echo 'par 0-2'; echo '<br/> PAR: '.$par; break; case ($par >= 3 && $par <= 5): echo 'par 3-5'; echo '<br/> PAR: '.$par; break; case ($par >= 6): echo 'par >=6'; echo '<br/> PAR: '.$par; break; default: echo 'ERROR: invalid PAR score passed. Please contact server administrator'; } ?> Switch statements are for comparing a single value, to another single value. Not for comparing ranges. If you want to do ranges you should just chain together some IF statements. if ($par >= 0 && $par <= 2){ .. } else if ($par >= 3 && $par <= 5){ ... } else if ($par >= 6){ ... } $par = isset($_GET['par'])?$_GET['par'] : ''; switch ($par) { case 0: case 1: case 2: echo 'par 0-2'; echo '<br/> PAR: '.$par; break; case 3: case 4: case 5: echo 'par 3-5'; echo '<br/> PAR: '.$par; break; default: echo 'par >=6'; echo '<br/> PAR: '.$par; } What do you get for that? Thanks very much all for helpful replies. All worked to some extent, i have gone with Psycho's method. However, still if no par is passed it echos 'par 0-2 PAR:' This closer than before certainly, but it still doesnt echo the default message 'ERROR: invalid PAR score passed. Please contact server administrator'... Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432218 Share on other sites More sharing options...
Eiseth Posted May 25, 2013 Share Posted May 25, 2013 You cannot pass null value on switch, try if else, if you still want to use switch try inserting them inside an if else $par = your value if (!empty($par)) { // insert your case statement here w/o the default: } else { // echo your default value here echo: 'ERROR: Invalid PAR'; } Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432219 Share on other sites More sharing options...
Psycho Posted May 25, 2013 Share Posted May 25, 2013 There isn't a NULL value being passes. The statement to set $par will either be the passed value or an empty string. Since the default is for values >= 6, use the statement to set $par accordingly $par = isset($_GET['par'])?$_GET['par'] : 6; Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432263 Share on other sites More sharing options...
Eiseth Posted May 26, 2013 Share Posted May 26, 2013 There isn't a NULL value being passes. The statement to set $par will either be the passed value or an empty string. Since the default is for values >= 6, use the statement to set $par accordingly $par = isset($_GET['par'])?$_GET['par'] : 6; $par = isset($_GET['par'])?$_GET['par'] : ''; AFAIK switch statement uses "==" instead of "===", so passing '' string is consider null Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432349 Share on other sites More sharing options...
kicken Posted May 26, 2013 Share Posted May 26, 2013 (edited) $par = isset($_GET['par'])?$_GET['par'] : ''; AFAIK switch statement uses "==" instead of "===", so passing '' string is consider null The switch is comparing true to the result of the various expression. value/type of $par isn't involved in the final switch comparison. Setting $par to '' would cause it to match the first case because $par >= 0 && $par <= 2 would be true. The empty string would get converted to the integer 0 for that comparison. *side note: The above area of confusion is why I generally do not like these types of switch statements and avoid using/recommending them. Edited May 26, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432406 Share on other sites More sharing options...
mastubbs Posted May 28, 2013 Author Share Posted May 28, 2013 There isn't a NULL value being passes. The statement to set $par will either be the passed value or an empty string. Since the default is for values >= 6, use the statement to set $par accordingly $par = isset($_GET['par'])?$_GET['par'] : 6; Thanks all. Actually i only wanted to echo error message if no par was passed at all. psychos method almost works except it echos the default message for par >= 6. I adapted it as below which works in my case because par 99 is not a real par number. probably not the most elegant way but its worked this time. $par = isset($_GET['par'])?$_GET['par'] : 99; switch (true) { case ($par >= 0 && $par <= 2): echo 'par 0-2'; echo '<br/> PAR: '.$par; break; case ($par >= 3 && $par <= 5): echo 'par 3-5'; echo '<br/> PAR: '.$par; break; case ($par >= 6 && $par != 99): echo 'par >=6'; echo '<br/> PAR: '.$par; break; case ($par = 99): echo 'ERROR: invalid PAR score passed. Please contact server administrator'; break; default: echo 'ERROR: invalid PAR score passed. Please contact server administrator'; } Quote Link to comment https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/#findComment-1432771 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.