Hmm, don't know PHP but it might be something to do with floats (numbers that have really long decimal places). Basically, when you do some math to a number that involves a decimal place, there's usually some weird precision going under the hood.
Gonna paste in this excellent answer by catalin dot luntraru that I found from the PHP docs:
$x = 8 - 6.4; // which is equal to 1.6 $y = 1.6; var_dump($x == $y); // is not true
PHP thinks that 1.6 (coming from a difference) is not equal to 1.6. To make it work, use round()
var_dump(round($x, 2) == round($y, 2)); // this is true
This happens probably because $x is not really 1.6, but 1.599999.. and var_dump shows it to you as being 1.6.
And with your example, comparing the rounded numbers will hit the final if statement:
if
This is kinda awkward because now I'm not sure if I can rely on my code
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hmm, don't know PHP but it might be something to do with floats (numbers that have really long decimal places). Basically, when you do some math to a number that involves a decimal place, there's usually some weird precision going under the hood.
Gonna paste in this excellent answer by catalin dot luntraru that I found from the PHP docs:
And with your example, comparing the rounded numbers will hit the final
ifstatement:This is kinda awkward because now I'm not sure if I can rely on my code