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. :(
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):".
switch(0) will always execute the block of code associated with the case 0: block; still, here there's no actually executed code - both cases are empty.
C++ compilers can turn switch statements into a jump table (i.e. construct an array of jump offsets, then take the value and use it as an index into the table). This is O(1).
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 {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With