Reputation: 1837
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. :(
Upvotes: 10
Views: 18477
Reputation: 42140
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 {
}
Upvotes: 9
Reputation: 49089
You are using the switch
incorrectly.
Your code is understood 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)
is comparing $starost_vozila
to either TRUE
or FALSE
, which is not what you want to do.
I suggest using if
/elseif
rather than switch
in this case. For example:
if ($starost_vozila == 0) {
...
} else if ($starost_vozila < 6) {
...
} else if ($starost_vozila > 5 && $starost_vozila < 11) {
...
}
Upvotes: 3
Reputation: 732
using Switch is like == in "if statement" and not === (doesn't check for type). Only empty string or NULL would result in an Integer 0 - other than that, they are considered string 0.
Upvotes: 0