Solved

Warning: number_format() expects parameter 1 to be double

Posted on 2014-03-15
6
2,007 Views
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="$credit[0]";
	$credit = str_replace('-','', $credit);
 	$credit=number_format($credit, 2, '.', '');

Open in new window

Thanks for the help,
Sas
0
Comment
Question by:sasnaktiv
  • 4
  • 2
6 Comments
 
LVL 109

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.
0
 
LVL 1

Author Comment

by:sasnaktiv
ID: 39931473
Hi Ray,
Thanks for the help.

var_dump($credit)

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

Sas
0
 
LVL 109

Accepted Solution

by:
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;
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 109

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!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 1

Author Closing Comment

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

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

Sas
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39931615
Thanks for the points.  The definition of what is empty() is available here:
http://php.net/manual/en/function.empty.php
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.htaccess 5 33
PHP Sessions in WordPress 8 29
javascript: add id amounts 5 46
How do I fix this UPDATE error? 7 20
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

816 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now