krifur
krifur

Reputation: 890

strange beahviour with a php switch?

A sample of what I'm trying to do will be more explicit:

   var_dump($opti_point); //int 0

    if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

    switch ($opti_point) {

        case ($opti_point>= 0 && $opti_point < 25):
            $test = 0;
            break;

        case ($opti_point >= 25 && $opti_point < 50):
            echo 'we are in this case'; // This case is called !
            $test = 2;
            break;

        default:
            test = 0;
            break;
    }

Is there a trick here ?

thx

Upvotes: 0

Views: 131

Answers (7)

Fenton
Fenton

Reputation: 250922

You are not comparing what you think you are comparing. This is the code I think you want.

var_dump($opti_point); //int 0

if ($opti_point>=0 && $opti_point < 25) {
    $test = 0;
    echo 'You are now here!';
} elseif ($opti_point >= 25 && $opti_point < 50) {
    $test = 2;
} else {
    test = 0;
}

In your example, you are comparing the result of the logical statement...

($opti_point>=0 && $opti_point < 25) // true

To the value of $opti_point

0 // false

So PHP is actually converting what you think in an integer into a boolean to compare it with the result of the conditional statement.

Upvotes: 1

Nemoden
Nemoden

Reputation: 9056

You have a misunderstanding on how switch-case works. Case DOES NOT TEST YOUR EXPRESSION TO BE boolean TRUE!

It compares its value to 'switch' value!

Here is an explanation:

$opti_point>= 0 && $opti_point < 25 evalutes to true which integer representation is 1 and since PHP can deal with types on it's own, it turnes true to 1 and compares it with value in switch which is 0

$opti_point >= 25 && $opti_point < 50 evaluates to false which is 0 as integer, so... that's your case ;)

Upvotes: 1

user860572
user860572

Reputation: 101

if this is the exact code then try this

var_dump($opti_point); //int 0

if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

switch ($opti_point) {

    case ($opti_point>= 0 && $opti_point < 25):
        $test = 0;
        break;

    case ($opti_point >= 25 && $opti_point < 50):
        echo 'we are in this case'; // This case is called !
        $test = 2;
        break;

    default:
        $test = 0;
        break;
}

Upvotes: 1

beardhatcode
beardhatcode

Reputation: 4753

I'll go true the code with you

   var_dump($opti_point); //int 0 , or false  --- you should use TRUE

    if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

    switch ($opti_point) { // chose the case that is $opti_point (0 or false)

        case ($opti_point>= 0 && $opti_point < 25): // true, so go to next
            $test = 0;
            break;

        case ($opti_point >= 25 && $opti_point < 50): //false si this is the wan I pick
            echo 'we are in this case'; // This case is called !
            $test = 2;
            break; // ingore the rest

        default:
            test = 0;
            break;
    }

you should use TRUE in the switch

Upvotes: 1

prodigitalson
prodigitalson

Reputation: 60413

You need to change the switch argument to true of false if you do comparison liek that in the cases.

Upvotes: 3

miguelSantirso
miguelSantirso

Reputation: 1253

I think that's a very bad way to use a switch statement, you should not put conditional sentences in the cases... In fact, I'm sure that that would be illegal in other languages and I'm not sure that it should work in PHP. Use a number of concatenated if-else conditions instead:

if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

Upvotes: 1

Jon Gjengset
Jon Gjengset

Reputation: 4236

You cannot put comparisons inside "case" unfortunately... A switch is only used when a value can have one of a limited number of values like so:

switch ( $val ) {
  case 1:
    echo "Got 1";
    break;
  case 2:
    echo "Got 2";
    break;
  default:
    echo "Got invalid value";
}

A workaround would be to use:

switch (true) {
    case ($opti_point>= 0 && $opti_point < 25):
        $test = 0;
        break;

    case ($opti_point >= 25 && $opti_point < 50):
        echo 'we are in this case';
        $test = 2;
        break;

    default:
        test = 0;
        break;
}

Which will work, but is a bit ugly...

Also, you're missing a single quote in echo we are in this case'; which should be echo 'we are in this case';

You should be using an if instead =)

Upvotes: 3

Related Questions