We are currently migrating from PHP4 to PHP5 and have discovered an issue.
In PHP4, when we used the number_format() function on an empty string the output would be 0. However, in PHP5, running the same empty string through the number_format() function results in output of NULL. This has a ripple effect on a lot of our code that is not expecting NULL.
Is there a best-practice solution for this? Idea开发者_Go百科lly I'd like to be able to make the change at the number_format() call so that empty strings return 0 instead of NULL and not have to check all the possible places where the output may be used.
Why not just check for an empty var before you pass it to number_format
?
number_format(empty($var) ? '0' : $var);
Empty isn't the only issue. Usually, any non-numeric should be 0.
number_format(is_numeric($var) ? $var : 0);
will render anything not a number to be zero.
精彩评论