Warning: number_format() expects parameter 1 to be double

Posted on 2014-03-15
Last Modified: 2014-03-15
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,
Question by:sasnaktiv
  • 4
  • 2
LVL 110

Expert Comment

by:Ray Paseur
ID: 39931458
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.

Author Comment

ID: 39931473
Hi Ray,
Thanks for the help.


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

LVL 110

Accepted Solution

Ray Paseur earned 500 total points
ID: 39931489
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;
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 110

Expert Comment

by:Ray Paseur
ID: 39931492
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!

Author Closing Comment

ID: 39931574
Thanks Ray,
I was successful with something similar, but your approach is more efficient.

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

LVL 110

Expert Comment

by:Ray Paseur
ID: 39931615
Thanks for the points.  The definition of what is empty() is available here:

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question