【问题标题】:PHP Division by Zero [closed]PHP除以零[关闭]
【发布时间】:2011-06-23 09:17:14
【问题描述】:

我有以下 PHP 代码来显示我的标签云。似乎如果我没有至少两个相同的标签,我会收到一条警告消息,提示警告:除以零(如下所示)。

有人可以帮我修复这段代码吗?谢谢!

<?php
// define variables
$fontSizeUnit = "px";
$maxFontSize = 40;
$minFontSize = 10;
$tagsToDisplay = 100;
$tagDivider = " ";

// font size range
$fontSizeSpread = $maxFontSize - $minFontSize;

// create blank arrays
$tagArray = array();
$tempTagArray = array();

// DB: get all public tags
$result = mysqli_query($conn, "SELECT Tags FROM blog WHERE Tags != '' AND IsPublic = 1 ORDER BY RAND()")
or die($dataaccess_error);

if(mysqli_num_rows($result) > 0 )
{
    // loop through results
    while($row = mysqli_fetch_array($result))
    {
        // split the results
        $tempStringArray = preg_split("/,/", $row['Tags']);

        // loop through all items of this string array
        for ($a = 0; $a < count($tempStringArray); $a++) 
        {
            // convert to lowercase
            $tempStringArray[$a] = strtolower($tempStringArray[$a]);

            // check if it exists in tag array
            if (empty($tagArray[$tempStringArray[$a]])) 
            {
                // if it doesn't exist, create it with value 1
                $tagArray[$tempStringArray[$a]] = 1;
            } 
            else 
            {
                // if it does exist, increase the value by 1
                $tagArray[$tempStringArray[$a]] += 1;
            }
        }
    }

    // store to temporary array and sort descending
    arsort($tagArray);
    $numberOfTags = 0;

    foreach ($tagArray as $key => $val) 
    {
        $numberOfTags++;

        if ($numberOfTags > $tagsToDisplay) 
        {
            break;
        }

        $finalTagArray[$key] = $val;
    }

    ksort($finalTagArray);

    $maxTagCount = max($finalTagArray);
    $minTagCount = min($finalTagArray);

    $tagCountSpread = $maxTagCount - $minTagCount; // <<== Problem here...

    $unitsPerCount = $fontSizeSpread/$tagCountSpread;

    // function to calculate the font size
    function calcSize($thisTagCount) {

        // import necessary global variables
        global $minTagCount, $minFontSize, $fontSizeUnit, $unitsPerCount;

        // calculate font size
        $thisFontSize = $minFontSize+($unitsPerCount*($thisTagCount-$minTagCount));

        // round font size and add units
        $thisFontSize = round($thisFontSize) . $fontSizeUnit;

        // return font size
        return $thisFontSize;

    }

    // echo out the resulting tags
    $b = 1;
    foreach ($finalTagArray as $key => $val) 
    {
        echo "<a href='snippets-by-tags.php?tag=".urlencode($key)."' style='font-size: ";
        echo calcSize($val);
        echo "'>" . htmlentities($key) . "</a>";

        if($b != count($finalTagArray)) 
        {
            echo " " . $tagDivider . " ";
        }
        $b++;
    }
}
else
{
    echo '<a href="#">none found ...</a>';
}
?>

【问题讨论】:

    标签: php divide-by-zero


    【解决方案1】:

    您应该检查$tagCountSpread 是否为0,显然除以0 = 无限。 (因此您收到错误)。这可能是一个快速的解决方案,但您应该真正为您的应用考虑合适的解决方案。

     if ($tagCountSpread <= 0) $tagCountSpread = 1;
     $unitsPerCount = $fontSizeSpread/$tagCountSpread;
    

    【讨论】:

      【解决方案2】:

      您的问题实际上出在这一行:

      $unitsPerCount = $fontSizeSpread/$tagCountSpread;
      

      如果 $tagCountSpread 为零,则这是除以零。当$maxTagCount$minTagCount 相同时,就会发生这种情况。

      你应该提防这个:

      if ($tagCountSpread != 0)
      {
        $unitsPerCount = $fontSizeSpread / $tagCountSpread;
      }
      else
      {
        // sensible recovery code
      }
      

      【讨论】:

        【解决方案3】:
        // ...
        
        $minTagCount = min($finalTagArray);
        
        if(($tagCountSpread = $maxTagCount - $minTagCount) === 0){
        
            // do something else when its zero; throw an exception, throw a party... whatever
        
        }
        
        // otherwise continue
        
        $unitsPerCount = $fontSizeSpread/$tagCountSpread;
        
        // ...
        

        【讨论】:

          【解决方案4】:

          PHP 将显示它发生在哪一行,找出它被零除的原因并修复它(可能使用一些条件)。

          【讨论】:

            【解决方案5】:

            也许你的问题是下面这行:

            $unitsPerCount = $fontSizeSpread/$tagCountSpread;

            而不是上一个。您应该检查 $tagCountSpread 是否为 NULL (0),如果不是,请进行除法:

            if($tagCountSpread != 0)
            {
                 $unitsPerCount = $fontSizeSpread/$tagCountSpread;
            }else{
                 ///something
            }
            

            【讨论】:

              猜你喜欢
              • 2011-07-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-07-30
              • 1970-01-01
              • 1970-01-01
              • 2021-02-26
              相关资源
              最近更新 更多