Solved

Warning: number_format() expects parameter 1 to be double

Posted on 2014-03-15
6
1,961 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 108

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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 look for a specific file type in a local or remote server directory using PHP.

910 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

18 Experts available now in Live!

Get 1:1 Help Now