Solved

Warning: number_format() expects parameter 1 to be double

Posted on 2014-03-15
6
2,408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
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.
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 110

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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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!
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 110

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

635 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