I have got a strange problem.This is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$fp=fopen("test.txt","w",true);
if($fp)
{
$start=time();
for($i=0;$i<=17800;$i++)
{
fwrite($fp,"bandwith");
}
fclose($fp);
$stop=time();
$diff=round($stop-$start,2);
$fsize=round(filesize("test.txt")/1024,2);
$bandwidth=$fsize/$diff;
echo "<strong>The speed of your broadband is</strong> ".$bandwidth;
}
else
{
echo ("problem with permission");
}
?>
</body>
</html>
I am actually trying to calculate the bandwidth.The error I get is "Division by zero" on the $bandwidth calculation part.The strange part is that when I keep on refreshing the window I suddenly get the answer then again the problem comes during the next refresh.
Extra info:
I found out that the error occurs when the $start and $stop are same.Now my question is:Is localhost so damn fast to finish the text file opening and writing part within the same time resulting in division my zero error?I also checked the text file and it contains the data and it is deleted and written every other time.I just want a clear explanation of the reason behind it.