0 in the switch housing?

Sorry, maybe a dumb question, but I have a HUGE problem with one case where I have an int variable with a 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; 

The switch will continue more, but here is my problem, in this piece of code.

If I do not put "case (0):" and use this:

 case ($starost_vozila >= 0 && $starost_vozila < 6): 

Then the next next case will somehow become active, and it will print out "$ eko_taksa = 667;".

This is all a problem when "$ starost_vozila = 0", but when this number is less than 6 than this example above.

Every var here is an int. Everything works fine, except when "$ starost_vozila = 0" and when I use the "case ($ starost_vozila> = 0 & $ starost_vozila <6):".

I have no idea what's going on ... Oo

Sorry if this is a stupid question. :(

+6
php switch-statement
source share
3 answers

switching cases do not accept statements that need to be evaluated. They take simple strings, booleans, or numbers to compare with

so say what do you have

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

you expect this case to be launched, but it’s not, and that’s why

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

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

0 != true , so the case fails

you need to use

 if() { } else if() { } else { } 
+6
source share

You are using switch incorrectly.

Your code is clear like this:

 if ($starost_vozila == 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; } } else if ($starost_vozila == ($starost_vozila < 6)) { //not what you want!!! 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; } } else if ($starost_vozila == ($starost_vozila > 5 && $starost_vozila < 11)) { //not what you want!!! 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; } } 

For example, ($starost_vozila < 6) will become either TRUE or FALSE . $starost_vozila == ($starost_vozila < 6) compares $starost_vozila with TRUE or FALSE , which you don't want to do.

In this case, I suggest using if / elseif rather than switch . For example:

 if ($starost_vozila == 0) { ... } else if ($starost_vozila < 6) { ... } else if ($starost_vozila > 5 && $starost_vozila < 11) { ... } 
+3
source share

using Switch is similar to == in "if statement", not === (does not check type). Only an empty string or NULL will result in an integer 0 - in addition, they are considered a string 0.

0
source share

All Articles