函数抛出零错误

时间:2017-01-25 21:20:36

标签: php

首先,我搜索了SO,并且没有其他建议好运,或者由于OP问题的复杂性我不明白。

我有两个函数正在抛出Division By Zero警告。

我该如何解决这个问题?我已经尝试使用isset(),empty()来验证逻辑,但似乎还在使用它。

有人可以告诉我哪里出错了吗?

我感谢任何帮助:)

function roundUpToAny($MaterialCountNeeded, $CoverageMinimum) {
    if ( !isset($MaterialCountNeeded) AND !isset($CoverageMinimum) ) {
        $ReturnedValue = 0;
    }
    if ($MaterialCountNeeded === 0 & $CoverageMinimum === 0 || $CoverageMinimum === 0) {
        $ReturnedValue = 0;
    }
    else {
    $ReturnedValue = (ceil($MaterialCountNeeded) % $CoverageMinimum === 0) ? ceil($MaterialCountNeeded) : round(($MaterialCountNeeded + $CoverageMinimum / 2) / $CoverageMinimum) * $CoverageMinimum;
    }
    return $ReturnedValue;
}


function SumQuantityNeeded($Count, $Coverage) {
    if ($Count > 0) {
        $OrderMaterialCount = roundUpToAny($Count, $Coverage);
        $MinimumUnitAmount = $OrderMaterialCount / $Coverage;
        return round($MinimumUnitAmount,$Coverage);
    } else {
        return $MinimumUnitAmount = 0;
    }
}

来自MinimumUnitAmount = $OrderMaterialCount / $Coverage;$ReturnedValue = (ceil($MaterialCountNeeded) % $CoverageMinimum === 0) ? ceil($MaterialCountNeeded) : round(($MaterialCountNeeded + $CoverageMinimum / 2) / $CoverageMinimum) * $CoverageMinimum;

的错误

这些功能是' Helper'功能。其他功能使用这些,所以我可能正在寻找错误的桶......

1 个答案:

答案 0 :(得分:1)

在第一个中,您似乎只需检查第二个参数是否为零。

function roundUpToAny($MaterialCountNeeded, $CoverageMinimum) {
    if (is_numeric($CoverageMinimum) && $CoverageMinimum > 0) {
        return (ceil($MaterialCountNeeded) % $CoverageMinimum === 0) ? ceil($MaterialCountNeeded) : round(($MaterialCountNeeded + $CoverageMinimum / 2) / $CoverageMinimum) * $CoverageMinimum;
    }

    return 0;
}

在第二步中,您应该检查$Coverage而不是$Count

function SumQuantityNeeded($Count, $Coverage) {
    if ($Coverage > 0) {
        $OrderMaterialCount = roundUpToAny($Count, $Coverage);
        $MinimumUnitAmount = $OrderMaterialCount / $Coverage;
        return round($MinimumUnitAmount, $Coverage);
    }

    return 0;    
}

我还简化了一些功能。