floatval()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

floatval()

Post by Cucumberdude »

So I'm fetching data from a webpage and trimming it using this code:

Code: Select all

$getposts = substr($content, $ppos+19, (strpos($content, " ("))-($ppos+19));
		
echo($getposts."|".floatval($getposts)."<br><br>");
The echo is for testing purposes. The script echos this:

Code: Select all

somenumber|0
'somenumber' being the number im trying to fetch. so my question is, why does floatval keep returning zero?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: floatval()

Post by Jonah Bron »

Problem might be that you're trying to convert a string directly to a float. To really know what the problem is, we'd have to know what the actual value of $getposts is. Use var_dump() on it by itself.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: floatval()

Post by Christopher »

The main reasons that floatval() returns zero is because there are non-numeric, non-whitespace characters at the beginning of the string ... or if the value is an array.
(#10850)
Post Reply