未定义函数 (PHP)

undefined function (PHP)

如果有人帮我找出这段代码中的错误,我将不胜感激:

函数 'freightPrice'(如下所示)位于名为 'freight.php' 的文件中,该文件位于 'main.php' 上的 required_once。 当我从 'main.php' 调用函数 'freightPrice' 时,我收到以下消息:

main.php - "Undefined function 'FreightPrice'".

freightPrice.php - "Variable 'FreightPrice' might have not been defined".

freightPrice.php - "Undefined variable 'FreightPrice'".

<?php

// file freightPrice.php

// FREIGHT CALCULATION

function FreightPrice($weight) {

    $PriceList = array(
        0 => 0,
        1 => 50.65,
        2 => 52.51,
        3 => 55.47,
        4 => 58.43,
        5 => 61.39,
        6 => 63.21,
        7 => 66.01,
        8 => 68.82,
        9 => 71.63,
        10 => 74.40
     );

    for ($i = 0; $i <= 66; $i++) {
        if ($i == round($weight, 0)) {
            $freightPrice = $PriceList[$i];
            break;
        }
    }

    return $freightPrice;

}

freightPrice(60);
echo $freightPrice;

?>

改成这个。注意函数名称上的大写 F 以修复第一个错误。变量 $freightPrice 仅存在于函数范围内,因此您需要将其 return 转换为当前范围内变量的新版本。

$freightPrice = FreightPrice(60); 
echo $freightPrice;

从您的代码片段中不太确定您要实现的目标...

首先,

freightPrice(60); //doesn't point to a valid function (case-sensitive).
echo $freightPrice; //this is undefined in a global scape

我倾向于使用 include();代替函数。

因此,如果您想测试它,只需

include("freightPrice.php");
echo FreightPrice(60);