Search code examples
phpvariablesinteger

PHP string with decimals to number


So when I try the following:

$a = '1.00';
$b = '1.01';

if($a < $b){
    print 'ok';
}

This works fine. But when I retrieve these variables from an xml file. the strings are EXACTLY the same, but for some reason, the if function doesn't work right. So I assume I have to convert the strings to a number. But when I do so, the decimals get removed.

Is my assumption correct? If so, how do i solve it?

If not, what's the problem?

Thank you!


Solution

  • $a = (float) $a;
    $b = (float) $b;
    

    Related reading: http://php.net/manual/en/language.types.type-juggling.php