If statements comparison integers
I have some if statements that read a variable from my mysql database and then echo out a number based on that variable. For some reason, the code below only echos out "1" and I dont know why. ($x is 670, but it still echos out 1)
CODE:
//connect to database and assign $x a int from database
if ($x<=100){
echo 1;
}
if ($x>=500 && $x<1000){
echo 2;
开发者_运维技巧}
if ($x>=1000 && $x<2500){
echo 3;
}
if ($x>=2500 && $x<5000){
echo 4;
}
if ($x>=5000 && $x<7500){
echo 5;
}
if ($x>=7500 && $x<100000){
echo 6;
}
if ($x>=10000){
echo 7;
}
EDIT: I did something stupid in my code with connecting to the DB. Thanks for your help anyway. I appreciate it.
It appears it's not the value you're expecting (int value of 670).
- Less than or equal to 100? Yes: echo 1. This tells you that indeed the value is not an int value of 670.
- The rest fail their boolean tests, as they don't pass those conditions.
To troubleshoot, try:
echo $x;
Perhaps explicitly cast that value as/after it comes out of your call to MySQL:
$x = (int)$x;
If this is indeed php, you $x surely is not 670.
See example here
Coudl you provide the code where you read x from the DB?
Try this:
$x = $x + 0.0;
if ($x<=100){
echo 1;
}
if ($x>=500 && $x<1000){
echo 2;
}
if ($x>=1000 && $x<2500){
echo 3;
}
if ($x>=2500 && $x<5000){
echo 4;
}
if ($x>=5000 && $x<7500){
echo 5;
}
if ($x>=7500 && $x<100000){
echo 6;
}
if ($x>=10000){
echo 7;
}
精彩评论