php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53667 number_format problem in empty string
Submitted: 2011-01-06 12:59 UTC Modified: 2011-01-06 15:20 UTC
From: diego at agudo dot eti dot br Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.3.4 OS: Debian-5
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: diego at agudo dot eti dot br
New email:
PHP Version: OS:

 

 [2011-01-06 12:59 UTC] diego at agudo dot eti dot br
Description:
------------
The problem occurs in version 5.3.4, the same situation does not create problems in version 5.2.6.

Passing an empty string as a parameter is generated an error that the parameter must be of type double the problem does not occur if the same string is not empty.

Test script:
---------------
Example 01:
Code: 
<?
 $x = "";
 echo "-->". number_format($x);
?>

Output:
Warning: number_format() expects parameter 1 to be double, string given in /www/htdocs/test.php on line 5
-->

Example 02:
<?
 $x = "0";
 echo "-->". number_format($x);
?>

Output:
-->0

Expected result:
----------------
I hope the developers look fondly and fix this problem. Although trivial, many other applications users can also get this problem.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-06 14:37 UTC] kalle@php.net
-Status: Open +Status: Bogus
 [2011-01-06 14:37 UTC] kalle@php.net
This is global for the parameter parsing API thats used across all functions within the PHP core. The thing is that "" cannot be converted to a double as there is no value, whereas "0" can be converted to a numeric value, "0" => 0 => (double) 0.

You should validate the input before sending it to the function, or alternatively forcing type casting which is an ugly hack but it fixes the problem for you:
number_format((double) $x);
 [2011-01-06 15:20 UTC] diego at agudo dot eti dot br
I understand, I'm using casting (float), just felt the difference from version 5.2.6 is not necessary to add any kind of casting. I figured it would be interesting to continue this way in future versions.

Thanks
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 10:01:29 2024 UTC