0

Can someone please help me figure out why this most basic of code isn't working in PHP? Have used PHP for years and have never seen this... It is consistent on every version of php as I tested it with the sandbox.onlinephpfunctions.com website, see link below the snippet of code.

All I want to do is to compare one number to another, but when the numbers contain floats things get weird.

<?php

$lside = 490.84;
$rside = 237.80 + 222.00 + 31.04;

if( $lside == $rside ){
   echo "they are equal\n";
} else {
   echo "not equal\n";
   echo "rside: [$rside]\n";
   echo "lside: [$lside]\n";
}

http://sandbox.onlinephpfunctions.com/code/6dee3a97f68a11e67fbaa8e5c157b827ecd47740

Help and thanks!

edit: here is how I ended up fixing this from the answers I received and looking into this further:

<?php

$lside = 490.84;
$rside = 237.80 + 222.00 + 31.04;

if( (string)$lside == (string)$rside ){
   echo "they are equal\n";
} else {
   echo "not equal\n";
   echo "rside: [$rside]\n";
   echo "lside: [$lside]\n";
}

So to compare two numbers in PHP, one safe way is to force them to be strings... go figure.

And I some additional testing to make sure this is true and always seems to work. All of these return "good":

if( "1" == "1" ) echo "1 - good\n";
if( "1.0" == "1" ) echo "2 - good\n";
if( "1" == "1.0" ) echo "3 - good\n";
if( "1.0" == "1.0" ) echo "4 - good\n";
if( "1.0" == "1.00000000" ) echo "5 - good\n";
if( "1.000000000000000" == "1.00000000" ) echo "6 - good\n";
if( "2" == (string)(.5+.5+1) ) echo "7 - good\n";
if( "2.000000000000000" == (string)(1+1) ) echo "8 - good\n";
if( "2.000000000000000" == (string)(1.0+1) ) echo "9 - good\n";
if( "2.4" == (string)(1.4+1) ) echo "10 - good\n";
if( "2.40000" == (string)(1.4+1) ) echo "11 - good\n";
if( "2.000000000000000" == (string)(.5+.5+1) ) echo "12 - good\n";
if( "1.5" == (string)(.5+.5+.5) ) echo "13 - good\n";
if( "1.500000000000000" == (string)(.5+.5+.5) ) echo "14 - good\n";
Patrick Steil
  • 453
  • 4
  • 7
  • Please use a title that describe the problem. Also, the description itself doesn't describe the problem. The reader needs to read the code and try to guess what your are trying to do. In this case it is relatively simple but being straight to the point will help you get more/better answers in the future. I guess this is why some people downvoted the question. – Gilles-Philippe Paillé Dec 17 '19 at 13:10
  • Thanks to those who answered. Sorry for the duplicate. I did try searching, but wasn't sure what the issue was, thus my question. I found another way also that seems to work and is more flexible: if( (string)$lside == (string)$rside ) seems to work. – Patrick Steil Dec 17 '19 at 13:27
  • Thanks @Gilles-PhilippePaillé - I edited it... I have posted very few questions on SO, so learning, thanks. – Patrick Steil Dec 17 '19 at 13:32
  • It is much clearer with the edits you made. Thanks. Regarding the solution you found, it might be slower and more error-prone than the answers below since you don't have direct control over the float->string conversion. I suggest to use Adrian's answer. – Gilles-Philippe Paillé Dec 17 '19 at 13:52

2 Answers2

2

You can try this :

if( bccomp($lside, $rside, 2) == 0 ){
   echo "they are equal\n";
} else {
   echo "not equal\n";
   echo "rside: [$rside]\n";
   echo "lside: [$lside]\n";
}

Because there's a complex thing when you compare float, you can read more here, and for the bccomp function you can read more here

Adrian Edy Pratama
  • 3,841
  • 2
  • 9
  • 24
0

Try This

$lside = 490.84;
$rside = 237.80 + 31.04 + 222;
$newRside = number_format($rside,2);

if( $lside == $newRside ){
   echo "they are equal\n";
} else {
   echo "not equal\n";
   echo "rside: [$rside]\n";
   echo "lside: [$lside]\n";
}
Blueberry
  • 31
  • 10
  • Thanks! Your answers and links led me a great understanding. Had never run into this with PHP. I don't have enough rep to upvote on my own question. That doesn't seem right :) I tried to upvote this one :) – Patrick Steil Dec 17 '19 at 13:33