Page 1 of 1

floatval()

Posted: Thu Jan 13, 2011 9:29 pm
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?

Re: floatval()

Posted: Thu Jan 13, 2011 11:44 pm
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.

Re: floatval()

Posted: Fri Jan 14, 2011 2:17 am
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.