【问题标题】:undefined function (PHP)未定义函数 (PHP)
【发布时间】:2015-04-19 18:09:20
【问题描述】:

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

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

main.php - “未定义函数'FreightPrice'”。

freightPrice.php - “变量 'FreightPrice' 可能尚未定义”。

freightPrice.php - “未定义的变量'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;

?>

【问题讨论】:

  • 更正:requere_once

标签: function include required


【解决方案1】:

改成这个。注意函数名称上的大写 F 以修复第一个错误。变量$freightPrice只存在于函数的作用域内,所以需要在当前作用域内将其返回到新版本的变量中。

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

【讨论】:

  • Mike 问题在于范围。非常感谢!
【解决方案2】:

从您的 sn-p 中不太确定您要达到的目标...

首先,

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);

【讨论】:

    猜你喜欢
    • 2012-02-09
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2019-08-11
    • 1970-01-01
    • 2011-10-25
    相关资源
    最近更新 更多