Warning: number_format() expects parameter 1 to be double

Hi Guys,
Can anyone tell me why I'm getting the following warning? It started happening when I moved to a dedicated server.
I'm running PHP 5.2

"Warning: number_format() expects parameter 1 to be double, string given in /DocumentName.php on line 6"

From this code:
	$credit="SELECT credit FROM $M_TableName WHERE UniqueID= '$UniqueID'";
	$credit=mysql_query($credit)or die(mysql_error());
	$credit=mysql_fetch_array($credit)or die(mysql_error());
	$credit = str_replace('-','', $credit);
 	$credit=number_format($credit, 2, '.', '');

Open in new window

Thanks for the help,
Who is Participating?
Ray PaseurConnect With a Mentor Commented:
So it looks like $credit is an empty string.  But number_format() is expecting numbers, not strings.  If $credit was a zero it could be converted to a floating point number (PHP would do this automatically) and then number_format() should be able to work correctly.

Try this after line 5:

if (empty($credit)) $credit = 0.0;
Ray PaseurCommented:
Please use var_dump($credit) after line 3 and post the output here, thanks.

You may also find that you want to add error_reporting(E_ALL) to the top of your PHP scripts.
sasnaktivAuthor Commented:
Hi Ray,
Thanks for the help.


Returns the following:
array(2) { [0]=> string(0) "" ["credit"]=> string(0) "" }

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Ray PaseurCommented:
You may also want to read this article.  At some point, PHP will eliminate the MySQL extension from the language, and you'll want to be prepared before that happens.  You may also want to upgrade your version of PHP.  PHP 5.2 is no longer supported at all, not even for security fixes!
sasnaktivAuthor Commented:
Thanks Ray,
I was successful with something similar, but your approach is more efficient.

if ($credit >'0' || $credit ==''){ $credit = 0.0;}

Ray PaseurCommented:
Thanks for the points.  The definition of what is empty() is available here:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.