Jump to content

Warning: Division by zero in /rate.php on line 64


supermanohar

Recommended Posts


I am getting Warning: Division by zero in /rate.php on line 64

<?php

function rateinstar($value)
{

preg_match_all('#([0-9]+)([0.0-9.9]+)|([0-9]+)#',$value,$tek);

if($tek[0][1] > 0)
{
$rate=$tek[0][0]/$tek[0][1];
}
else
{
$rate=0;
}

preg_match_all('#([0-9]+).([0-9]+)#',round($rate,1),$decm);

if($decm[2][0] != "")
{
$no=($decm[1][0]*2)+1;

for($i=1;$i<=10;$i++)
{
if($i<=$no){
if($i%2!=0)
echo '<span class="starin piece1"></span>';
else
echo '<span class="starin piece2"></span>';
}else{
if($i%2!=0)
echo '<span class="starin piece3"></span>';
else
echo '<span class="starin piece4"></span>';
}
}
}else{

for($i=1;$i<=10;$i++)
{
if($i<=$rate*2){
if($i%2!=0)
echo '<span class="starin piece1"></span>';
else
echo '<span class="starin piece2"></span>';
}else{
if($i%2!=0)
echo '<span class="starin piece3"></span>';
else
echo '<span class="starin piece4"></span>';
}

}

}
}


function rateintext($value)
{

preg_match_all('#([0-9]+)([0.0-9.9]+)|([0-9]+)#',$value,$tek);

$rate=$tek[0][0]/$tek[0][1];

preg_match_all('#([0-9]+).([0-9]+)#',round($rate,1),$decm);


return round($rate,1);
}

?>

 

Link to comment
Share on other sites

You could do something like this to set the rate to 0 if it's going to attempt to divide by 0.

 

$rate = $tek[0][1] != 0 ? $tek[0][0] / $tek[0][1] : 0;

 

But, you may wish to just return 0 if it's not going to work.

 

if ($tek[0][1] == 0)
    return 0;

$rate = $tek[0][0] / $tek[0][1];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.