View Full Version : problem in comparing values......
gurmeet
07-14-2010, 07:27 AM
hi friends... i need to compare two values , but i fails.....
value 1 : it is coming from database directly... the value is ( 0 , 1 or 4)any1
value 2 : it is the return value from a function ... the value is (0 or 1 only)
i want to compare if (value1 <= value2)
but it does not compares.. cz it takes value1 as string ...
so in cast the type into int.. but still it have same problem....
it works if i write the condiion : if(value1 == value2)
it works... but i need comparison of less than...
djr33
07-14-2010, 04:00 PM
Try forcing it to be a number. Here's an easy trick:
if ($v1+1 <= $v2+1) { ....
That's not very pretty, but it will do what you need.
hi friends... i need to compare two values , but i fails.....
value 1 : it is coming from database directly... the value is ( 0 , 1 or 4)any1
value 2 : it is the return value from a function ... the value is (0 or 1 only)
i want to compare if (value1 <= value2)
but it does not compares.. cz it takes value1 as string ...
so in cast the type into int.. but still it have same problem....
it works if i write the condiion : if(value1 == value2)
it works... but i need comparison of less than...
to be clear, you're saying that this (http://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting)doesn't work? it should:
<?php
if( (int)$value1 <= (int)$value2 ){ /* do stuff */ }
?>
djr33
07-14-2010, 05:03 PM
And that's probably a better way than what I suggested. The only advantage to mine is that you don't need to remember that syntax, just do an operation requiring numbers and go from there. But since traq's method is the correct way to set this, I'd suggest using it instead.
If type casting isn't working for some reason, you could also do a variation on djr's method:
<?php
// get your value from the function or DB call
$value = value_from_function();
// adding zero to the value doesn't change it, but should cast it as an integer
$value += 0;
// then use it normally
?>Of course, type casting should work just fine. Did I understand your first post correctly that it is not?
bluewalrus
07-15-2010, 02:01 AM
Is (int) a short hand way of settype?
hmm... I'm pretty sure it's a basic language construct, not "shorthand" for the function settype(). (I had actually never seen that function before.) It sure is less work, though. :D
Now this is crazy:
<?php
$foo = 10; // $foo is an integer
$str = "$foo"; // $str is a string
?>Kinda like the cheat djr suggested above.
djr33
07-15-2010, 07:48 AM
The other easy way to force a string is: $foo = $foo.'';
fileserverdirect
07-15-2010, 05:05 PM
The other easy way to force a string is: $foo = $foo.'';
Note that those are two single quotes. (I was about to post something saying you needed to close those double quotes :p)
djr33
07-15-2010, 05:47 PM
Double quotes would be fine. I've gotten into a habit of only (with rare specific exceptions) using single quotes, not double, in my PHP. But double does the same thing here.
gurmeet
07-16-2010, 06:33 AM
nithing works :(
facing same problem...
fileserverdirect
07-16-2010, 07:04 AM
Please post the code that you are using :)
Are you sure your DB queries and functions are returning values at all? Any (all) of the suggestions above should be working.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.