Jump to content

Issue with < operator and integer values


praefect

Recommended Posts

Goodday all,

Perhaps you can help shed some light on this:

[code]
        $query = "select count(clientid) as c from xxx where clientid='".$this->session->getclientid()."'";
        $cnt = $this->db->selectSQL($query);
$valid = $valid and $cnt['c']<=GSInfo::$maxGS;
[/code]

Now, the value of $cnt at this point is 22, and the value of GSInfo::$maxGS is 15.  For whatever reason, php is setting $valid to true, when clearly 22<=15 is false.  I thought perhaps that the sql query might be spitting out a string, but this would not be an issue in PHP correct?  Perhaps its a lack of understanding on my part on the behaviour of PHP vs C++/Java/Ada. 

Thanks for the help,
Rob
Link to comment
https://forums.phpfreaks.com/topic/34769-issue-with-operator-and-integer-values/
Share on other sites

i havent checked this out, but depends on the order of operators - ie, which gets dealt with first when calculating:

[code]
$valid = $valid and $cnt['c']<=GSInfo::$maxGS;
[/code]

it seems that, given the numbers you state, the above is being evaluated as ($valid and $cnt['c']) <= GSInfo::$maxGS; . try throwing in some brackets to specifically state your order:

[code]
$valid = $valid && ($cnt['c'] <= GSInfo::$maxGS);
[/code]
cool!

even still.i actually find it always pays anyway to use brackets - both from a debugging point of view, but also a portability one. There's countless occasions I can think of where I've been required to go through code written using one language putting brackets in for another language, due to the calculation order being different.

anyway - glad it helped!
cheers

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.