How I resolved the PHP error with comparing numbers using number_format()

Steve Sohcot
1 min readOct 8, 2020

Something wasn’t working correctly when I had to compare two numbers in PHP (it was a basic less than / greater than formula).

I was comparing 1,000 > 29 and it came back false. Obviously that wasn’t right! 😏

Upon further investigation, any number more than 1,000 caused the statement to return false. The comma was converting the value to just 1 (so it was evaluating 1 > 29 = false).

The problem was that I was using the PHP function number_format() before comparing the numbers.

The lesson learned: format the number after a numerical comparison.

<?php
$x = "1,000";
print number_format($x); // returns 1
?>

--

--