views:

28

answers:

2

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? Ideally 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.

Thanks in advance!

+1  A: 

Why not just check for an empty var before you pass it to number_format?

number_format(empty($var) ? '0' : $var);
ircmaxell
A: 

You may prefix the string with (int) to convert it to an integer:

number_format((int) $number);

This will deal with not only empty strings, but other malformed input, too.

nikic
This will fail when $number is a float. Cast to float, not int.
Charles