Basically, i've made a script that login to a website, get elements from the website, and divide one element with another. The problem is, i have already checked plenty of times, and the divisor is NOT zero. here is the snippet:
if($num==0)
{
echo "<td>".$estate_income."/".$num."</td>";
}
else
{
echo "<td>".$estate_income/$num."</td>";
}
this would output something like 50/2000
, which means the if statement is true, which means that $num is somehow equal to 0. if i try to divide the two variables with each other, it would output php warning diivision by zero
:
echo "<td>".$estate_income/$num."</td>";
What i ask now is, for a solution, maybe some error detection methods that could tell me what i am doing wrong. it's probably something very obvious that i have overlooked.
Thanks in advance!
Ahmad Albayati
try
{
echo "<td>".$estate_income/$num."</td>";
}
catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Outputs Warning: Division by zero in . . . on line 133
The variables i am trying to divide is within a foreach loop.
The $num variable is defined in a function that gets included:
function num_format($n)
{
$n=str_replace(",","",str_replace(" ","",$n));
if(strpos($n,".")===FALSE)
{
if(strpos($n,"K")!==FALSE)
{
$n=str_replace("$","",str_replace("K","000",$n));
}
elseif(strpos($n,"mil")!==FALSE)
{
$n=str_replace("$","",str_replace("mil","000000",$n));
}
elseif(strpos($n,"bil")!==FALSE)
{
$n=str_replace("$","",str_replace("bil","000000000",$n));
}
elseif(strpos($n,"tril")!==FALSE)
{
$n=str_replace("$","",str_replace("tril","000000000000",$n));
}
else
{
$n=str_replace("$","",$n);
}
}
else
{
$n=str_replace(".","",$n);
if(strpos($n,"K")!==FALSE)
{
$n=str_replace("$","",str_replace("K","00",$n));
}
elseif(strpos($n,"mil")!==FALSE)
{
$n=str_replace("$","",str_replace("mil","00000",$n));
}
elseif(strpos($n,"bil")!==FALSE)
{
$n=str_replace("$","",str_replace("bil","00000000",$n));
}
elseif(strpos($n,"tril")!==FALSE)
{
$n=str_replace("$","",str_replace("tril","00000000000",$n));
}
else
{
$n=str_replace("$","",$n);
}
}
global $num;
$num=$n;
}
Found the problem. As I have already mentioned, I got the values from a website. When I use var_dumb on the string, it outputs string(149) "202000"
. This actually had confused me, because the string/number was only 6 characters. so I decided to look at the source code, and I found this:
<spanstyle="white-space:nowrap;"><imgsrc="http: staticstorm8com="" vl="" images="" bloodpng?v="330"width="9"height="12"style="padding-right:2px"">202000<br></imgsrc="http:></spanstyle="white-space:nowrap;">
I got some other HTML elements in my variable when I was taking the values from the website.