Jump to content

Recommended Posts

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 by mastubbs
Link to comment
https://forums.phpfreaks.com/topic/278358-some-odd-problems-with-switch-case/
Share on other sites


$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?

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){ ... }

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';
    }

?>

 

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'...

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';
}

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; 

 

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

 

$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 by kicken

 

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';
    }
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.