Joe Smalley
Joe Smalley

Reputation: 313

Basic PHP logic problem

$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.

Upvotes: 1

Views: 1371

Answers (4)

Henrik
Henrik

Reputation: 23

<?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!

Upvotes: 0

zaf
zaf

Reputation: 23244

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.

Upvotes: 2

kingjeffrey
kingjeffrey

Reputation: 15270

As an alphabetical comparison, the following statement is true:

"800" > "2400"

(because 8 is greater than 2)

Upvotes: 0

MartyIX
MartyIX

Reputation: 28648

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.)

Upvotes: 3

Related Questions