8

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!

1
  • Are you converting them to a double? Commented Jun 11, 2012 at 12:17

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

so what's the difference between float and floatval?
When converting an object to a float, floatval will generate a notice, casting to float won’t.
-5

Do it like this:

floatval($string);

1 Comment

Your answer contains code without explanation. It's better to include details about how your answer answers the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.