php switch statement error on int = 0
Posted
by
Jagdeep Singh
on Stack Overflow
See other posts from Stack Overflow
or by Jagdeep Singh
Published on 2012-09-30T09:29:17Z
Indexed on
2012/09/30
9:37 UTC
Read the original article
Hit count: 339
php
|switch-statement
I am having a problem in php switch case.
When i set $number=0 it should run very first case but here this code returns 10-20K that is in second case.
I checked comparison operators, tested them in if else case they return correct values but here first case do not run on $number=0
Why is this happening ? php consider 0 as false or something wrong in code ?
Link to codepad paste http://codepad.org/2glDh39K
also here is the code
<?php
$number = 0;
switch ($number) {
case ($number <= 10000):
echo "0-10K";
break;
case ($number > 10000 && $number <= 20000):
echo "10-20K";
break;
case ($number > 20000 && $number <= 30000):
echo "20-30K";
break;
case ($number > 30000 && $number <= 40000):
echo "30-40K";
break;
case ($number > 40000 && $number <= 50000):
echo "40-50K";
break;
case ($number > 50000 && $number <= 60000):
echo "50-60K";
break;
case ($number > 60000 && $number <= 70000):
echo "60-70K";
break;
case ($number > 70000 && $number <= 80000):
echo "70-80K";
break;
case ($number > 80000 && $number <= 90000):
echo "80-90K";
break;
case ($number > 90000):
echo "90K+";
break;
default: //default
echo "N/A";
break;
}
?>
© Stack Overflow or respective owner