未定义的变量

时间:2011-04-18 03:37:44

标签: php

<?php
$name = $_GET['name'];
if ($_GET['size'] == "small") 
{
    $delivery = 5;
    $_GET['size'] = 5;
    $_GET['topping'] = 1;
    if ($_GET['deliverytype'] == "pickup") 
    {
        $total = $_GET['size'] + $_GET['topping'];
    } 
    elseif ($_GET['deliverytype'] == "delivery") 
    {
        $total = $_GET['size'] + $_GET['topping'] + $delivery;
    }
}
echo "Dear " . $name. " your " . $_GET["size"] . " pizza has been ordered.";
echo "You Total is " . $total;

?>

我收到错误: 注意:未定义的变量:C:\xampp\htdocs\process.php on line 57中的总数 任何人都可以建议,为什么会这样?

4 个答案:

答案 0 :(得分:3)

如果$_GET['size']不是“小”,则$total将是未定义的。同样,如果大小“小”,但“deliverytype”既不是“交付”也不是“提货”。

答案 1 :(得分:1)

要解决此问题,请在第1行添加:

$total = 0;

要编写好的PHP,您必须先定义变量,否则您将有机会遇到空值。

答案 2 :(得分:1)

我觉得自己有点牵手;),所以这就是你可能想要大部分价格计算功能的样子。我可以告诉你,对于实际的编程来说,这可能是非常新的,所以......我认为看到一些能够为你描绘整个画面的东西是有帮助的。

<?php
//These are some dummy variables, these 4 lines
//are pretend data that are theoretically sent form your form
$_GET['name'] = 'John';
$_GET['size'] = 'small';
$_GET['topping'] = 1;
$_GET['deliverytype'] = 'pickup';

//Typically you want to initialize all of your variables
//you're going to use at the top
$name = $_GET['name'];
$size = $_GET['size'];
$total = 0;

//All of the prices are sequentially added to the total
$total += calculate_size_cost();
$total += calculate_topping_cost();
$total += calculate_delivery_cost();

//And THEN, since the data exists, 100% certain it exists
//, this will output your desired information, with no missing variable stuff.
echo "Dear $name  your {$_GET["size"]} pizza has been ordered.";
echo "You Total is $ $total";

//If the size isn't small or large, this will return 5000
//You have to EXPLICITLY state everything that can happen
//If something unexpected happens, you have to place it after
//an "ELSE"
function calculate_size_cost(){
    if($_GET['size'] == "small")
    return 5;
    else if($_GET['size'] == "large")
    return 10;
    else
    return 5000;
}

function calculate_topping_cost(){
    //This is already a number, so, I'm assuming it's the topping price
    return $_GET['topping'];
}

function calculate_delivery_cost(){
    //Will add delivery cost, otherwise this will be zero if
    //it's not pickup
    $delivery_cost = 0;
    if($_GET['deliverytype'] == "pickup"){
        $delivery_cost += 5;
    }
    return $delivery_cost;
}

答案 3 :(得分:0)

当然,问题在于$ total未被声明。与任何编程语言一样,必须在使用之前声明变量。 PHP的独特之处在于设置变量将声明和设置该变量,这意味着您只需少一步。所以,你有三个选择。

在第一行中,执行$total = "";这会将变量设置为空字符串。这是无效的,因为无论是否有总价格,都会打印信息。

您还可以确保每个代码路径以某种方式定义$total。这将确保它永远不会被定义。这与上面的答案类似,但有助于防止错误检查,因为它总是被设置为有效值(尽管仍建议进行错误检查)。

最后一个也是最好的方法是改变

的最后一行

echo "Dear " . $name. " your " . $_GET["size"] . " pizza has been ordered.";
              echo "You Total is " . $total;

类似

if(isset($total))
{
    echo "Dear " . $name. " your " . $_GET["size"] . " pizza has been ordered.";
              echo "You Total is " . $total;
}
else
{
    echo "There was an error in your order. Please try again.";
}

这将检查在“完成”订单之前是否生成了总值,这样就无需对代码进行大量更改或进行大量错误检查。当然,使用isset()进行错误检查并不理想,但应该可以正常工作。