tags:

views:

141

answers:

4
$this->totplpremium is 2400
$this->minpremiumq is 800

So why would this ever return true?!

if ($this->totplpremium < $this->minpremiumq){

The figures are definitely correct and I am definitely using the 'less than' symbol. I can't work it out.

+3  A: 

Maybe there's some kind of conversion problem. Try use

var_dump($this->totplpremium);
var_dump($this->minpremiumq);
if ($this->totplpremium < $this->minpremiumq){
  ...
}

to see if the datatypes are allright

EDIT: There are tools that enables you to debug your code more easily than using debugging outputs - http://xdebug.org/ (an extension for PHP that enables you debugging) and http://en.wikipedia.org/wiki/PHPEd (It's commercial. I don't know if there's an alternative.)

MartyIX
Could be strings with whitespace. `2400 > 800`, `"2400" > "800"`, but `"2400 " < "800 "` - they won't get type-juggled to int because of the spaces, and lexicographically 2400 is smaller.
Tgr
2400 *IS* less than 800 when compared as strings.
Salman A
@Salman A: not it isn't. From the [documentation](http://www.php.net/manual/en/language.operators.comparison.php): *If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.*
Tgr
@MartyIX: Xdebug + Notepad++ + Xdebug helper for Firefox is a lightweight opensource debugging environment. All the big IDEs like Eclipse support Xdebug too, of course.
Tgr
@Tgr: very true but, as you mentioned above, its possible to confuse php into using string comparison for numbers.
Salman A
That's solved it - $this->totplpremium was a string. I had previously used "number_format()" on it.
Joe Smalley
A: 

As an alphabetical comparison, the following statement is true:

"800" > "2400"

(because 8 is greater than 2)

kingjeffrey
It is false, actually. PHP type-juggles string containing pure numbers into numbers before doing the comparison.
Tgr
+2  A: 

Try wrapping the 'numbers' with intval:

if (intval($this->totplpremium) < intval($this->minpremiumq)){
//...
}

If this works as expected then you really need to check what types totplpremium and minpremiumq are by using gettype, for example:

print(gettype($this->totplpremium));
print(gettype($this->minpremiumq));

With that info you should be able to pinpoint your error.

zaf
A: 
<?php

$totplpremium="2400 ";
$minpremiumq="800";

var_dump(($totplpremium < $minpremiumq)?true:false);
var_dump(((int)$totplpremium < (int)$minpremiumq)?true:false);

?>

I'd guess you should either double check where these values come from OR make sure they are integers.

Good luck coding!

Nada
Yes, the space after 2400 is there for a purpose: forcing them to be compared as strings!
Nada