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

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.

$x = "1,000";
print number_format($x); // returns 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store