0 in switch case?

php, switch-statement

Solution

switch cases don't take statements that need to be evaluated. They take simple strings, booleans or numbers to be compared against

so say you have

$x = 0

switch( $x ) {
    case( $x < 10 ):
    ...
    break;
}

you are expecting that case to run but it doesn't and here's why

`( $x < 10 )` evaluates to `true` so what you actually have is:

$x = 0

switch( $x ) {
    case true: //!!!
    ...
    break;
}

`0 != true` so the case fails

you need to use

if() {

} else if() {

} else {

}

Problem

Sorry for maybe dumb question, but i have HUGE problem with one case when i have some int variable with value of 0(zero). ``` switch ($starost_vozila){ case (0): switch ($podaci['tip_motora']){ case ("motor1"): $eko_taksa = 485; break; case ("motor2"): $eko_taksa = 243; break; case ("motor3"): $eko_taksa = 121; break; case ("motor4"): $eko_taksa = 194; break; } break; case ($starost_vozila < 6): switch ($podaci['tip_motora']){ case ("motor1"): $eko_taksa = 485; break; case ("motor2"): $eko_taksa = 243; break; case ("motor3"): $eko_taksa = 121; break; case ("motor4"): $eko_taksa = 194; break; } break; case ($starost_vozila > 5 && $starost_vozila < 11): switch ($podaci['tip_motora']){ case ("motor1"): $eko_taksa = 667; break; case ("motor2"): $eko_taksa = 273; break; case ("motor3"): $eko_taksa = 136; break; case ("motor4"): $eko_taksa = 218; break; } break; ``` Switch continue more, but here is my problem, in this piece of code. If i dont put "case (0):" and use this: ``` case ($starost_vozila >= 0 && $starost_vozila < 6): ``` Then the case that is next will somehow become active and it will print out that "$eko_taksa = 667;". That is all problem when "$starost_vozila = 0" but when it is any number less then 6 than this case above works. Every var here is int. Everything works ok except when "$starost_vozila = 0" and when i use "case ($starost_vozila >= 0 && $starost_vozila < 6):". I have no idea what is going on... Oo Sorry if this is dumb question. :(

Original source