【问题标题】:How do I return a value from a second function in PHP?如何从 PHP 中的第二个函数返回值?
【发布时间】:2015-06-17 02:49:09
【问题描述】:

我是一个绿色新手,正在尝试用 PHP 编写一个简单的程序。我使用 HTML 表单让“用餐者”选择主菜,然后将选择发送给 PHP 程序。PHP 程序应该响应主菜选择,建议用餐者喝一杯,然后告诉用餐者什么主菜的费用,饮料是——包括税和小费。

第一个函数 select_beverage 接受主菜的选择并回显建议的饮料和饮料价格。然后它调用函数 wallet_buster 来计算账单的税收成本。然后Wallet_buster() 应该将应税成本返回给select_beverage(),而select_beverage() 又应将应税成本返回给名为 select_beverage 的变量。

我可以得到这个程序的简化版本,但这个野兽不行。我的老师建议我将 wallet_buster 返回的值保存为变量,然后在if/else 级联结束时返回。我试图在此代码中遵循该建议,但它不起作用。我也试过了

return wallet_buster($steak_price, $steak_drink_price);

在每个 if/else 函数中,但这也不起作用。

提前感谢您提供的任何启发!

<?php

echo "<h3>Thank you for dining at Elysium Excelsior</h3><br>";

function wallet_buster($entree_price, $drink_price) {
    $taxed_cost = 1.1 * ($entree_price + $drink_price);
    echo "<br/>";

    return $taxed_cost;   
}

function select_beverage($dinner) {
    $steak_price = 27.50;
    $steak_drink = "Justin Cabernet Sauvignon"; 
    $steak_drink_price = 13.15; 

    $salmon_price = 24.95;
    $salmon_drink = "Russian River Pinot Noir"; 
    $salmon_drink_price = 12.25; 

    $barbecue_pork_price = 22.99;
    $barbecue_pork_drink = "Dogfish Head 120 Minute IPA";
    $barbecue_pork_drink_price = 7.99;

    $chicken_price = 21.50;
    $chicken_drink = "Blue Nun Sauvignon Blanc";
    $chicken_drink_price = 12.25; 

    if ($dinner == "1") { 
        echo "The filet mignon pairs wonderfully with a glass of " . $steak_drink .
        at a price of $" . $steak_drink_price . ".<br/>";
        echo "<br/>";
        $receipt = wallet_buster($steak_price, $steak_drink_price);
}
else if ($dinner == "2") {    
    echo "A glass of " . $salmon_drink . " for a luxuriously priced $" .
                 $salmon_drink_price . " is a wonderful complement to our
    salmon."".<br/>"; 
    echo "<br/>";
    $receipt = wallet_buster($steak_price, $steak_drink_price);    
}
else if ($dinner == "3") { 
    echo "Try a pint of " . $barbecue_pork_drink . " for only $" .
    $barbecue_pork_drink_price . "."".<br/>"; 
    echo "<br/>";
    $receipt = wallet_buster($steak_price, $steak_drink_price);
}
else if ($dinner == "4") { 
    echo "Stiller and Meara invite you to try " . $chicken_drink . " at $" . 
    $chicken_drink_price . " per glass with the chicken!"".<br/>"; 
    echo "<br/>";
    $receipt = wallet_buster($steak_price, $steak_drink_price);
}
else {
    echo "Please select an entree from our drop-down menu and we will recommend
    a beverage suited to your choice.";
    echo "<br/>";
}

return $receipt;

}

$dinner = $_GET["entree"];

$big_deal_meal = select_beverage($dinner);

echo "<br>";
echo "We encourage our patrons to tip well; given your menu selections, we              `  `believe your bill should be : $" . (1.25 * $big_deal_meal);

?>

【问题讨论】:

  • 这个程序做了什么不是预期或不期望的行为?只是很难通过所有代码来发现问题——如果你能帮助我们解决你遇到的确切问题(某个变量没有正确打印等),那将是一个巨大的问题帮助。
  • 您可以根据颜色编码看到语法错误。
  • 您可能希望在四种情况下分别传入不同的变量:1:$steak_price, $steak_drink_price,2:$salmon_drink, $salmon_drink_price,3:$barbecue_pork_drink, barbecue_pork_drink_price,4:$chicken_drink, $chicken_drink_price..在每种情况下传递相同的未更改变量没有多大意义(因为现在所有 4 种情况都做同样的事情)。但也许你的问题是别的……?
  • 我还想指出,当涉及到 qoutes(开始/结束字符串)时,当前代码完全被破坏了。在这里粘贴代码时可能会出错?例子。 . " per glass with the chicken!"".&lt;br/&gt;"; 应该是 . " per glass with the chicken!&lt;br/&gt;";$steak_drink . at a price of $" 应该是 $steak_drink . "at a price of $"
  • 作为一个葡萄酒势利小人,我想指出黑比诺配鲑鱼是一个糟糕的选择。

标签: php function return


【解决方案1】:

您的代码大部分都在工作,您只是放错了一些引号和连接。在不应该的地方添加引号或忘记在需要的地方添加引号将导致 PHP 误解您的代码。您将来可能会考虑使用代码编辑器或 IDE 来避免这种情况。他们会突出显示您的代码,以在您犯错误(例如缺少报价)时提醒您。像 Netbeans 这样的 IDE 会不断检查您的代码是否存在语法错误。

在您的 PHP 配置中启用错误报告还会为您提供有关脚本中出现问题的有用提示。

这是工作代码:

<?php

echo "<h3>Thank you for dining at Elysium Excelsior</h3><br>";

function wallet_buster($entree_price, $drink_price) {

$taxed_cost = 1.1 * ($entree_price + $drink_price);
echo "<br/>";

return $taxed_cost;

}



function select_beverage($dinner) {

$steak_price = 27.50;
$steak_drink = "Justin Cabernet Sauvignon"; 
$steak_drink_price = 13.15; 

$salmon_price = 24.95;
$salmon_drink = "Russian River Pinot Noir"; 
$salmon_drink_price = 12.25; 

$barbecue_pork_price = 22.99;
$barbecue_pork_drink = "Dogfish Head 120 Minute IPA";
$barbecue_pork_drink_price = 7.99;

$chicken_price = 21.50;
$chicken_drink = "Blue Nun Sauvignon Blanc";
$chicken_drink_price = 12.25; 

if ($dinner == "1") { 
echo "The filet mignon pairs wonderfully with a glass of " . $steak_drink . "
at a price of $" . $steak_drink_price . ".<br/>";
echo "<br/>";
$receipt = wallet_buster($steak_price, $steak_drink_price);

}

else if ($dinner == "2") {    
echo "A glass of " . $salmon_drink . " for a luxuriously priced $" .
                 $salmon_drink_price . " is a wonderful complement to our salmon <br/>"; 
echo "<br/>";
$receipt = wallet_buster($steak_price, $steak_drink_price);    
}

else if ($dinner == "3") { 
echo "Try a pint of " . $barbecue_pork_drink . " for only $" .
$barbecue_pork_drink_price . ". <br/>"; 
echo "<br/>";
$receipt = wallet_buster($steak_price, $steak_drink_price);
}

else if ($dinner == "4") { 
echo "Stiller and Meara invite you to try " . $chicken_drink . " at $" . 
$chicken_drink_price . " per glass with the chicken! <br/>"; 
echo "<br/>";
$receipt = wallet_buster($steak_price, $steak_drink_price);
}

else {
echo "Please select an entree from our drop-down menu and we will recommend
a beverage suited to your choice.";
echo "<br/>";
}

return $receipt;

}

$dinner = $_GET["entree"];



$big_deal_meal = select_beverage($dinner);



echo "<br>";
echo "We encourage our patrons to tip well; given your menu selections, we believe your bill should be : $" . (1.25 * $big_deal_meal);
?>

另请注意,您不需要连接(使用“.”)文本和 html。仅当您在 PHP 代码中混合变量时。

所以:"&lt;span&gt;" . "I am a string" . "&lt;/span&gt;&lt;br&gt;"; 是不必要的。这:"&lt;span&gt;I am a string&lt;/span&gt;&lt;br&gt;"; 很好,更容易阅读。

【讨论】:

  • 感谢您对 IDE 的建议!顺便说一句,我们都错过了我在牛排晚餐中对每种晚餐的争论。当我得到最终建议的餐费时,我发现了这一点。
【解决方案2】:

这样的?

function select_beverage($dinner) {
    $steak_price = 27.50;
    $steak_drink = "Justin Cabernet Sauvignon"; 
    $steak_drink_price = 13.15; 

    $salmon_price = 24.95;
    $salmon_drink = "Russian River Pinot Noir"; 
    $salmon_drink_price = 12.25; 

    $barbecue_pork_price = 22.99;
    $barbecue_pork_drink = "Dogfish Head 120 Minute IPA";
    $barbecue_pork_drink_price = 7.99;

    $chicken_price = 21.50;
    $chicken_drink = "Blue Nun Sauvignon Blanc";
    $chicken_drink_price = 12.25;

    $selected_meal_price = NULL;
    $selected_drink_price = NULL;

    if ($dinner == "1") { 
        echo "The filet mignon pairs wonderfully with a glass of " . $steak_drink . "at a price of $" . $steak_drink_price . ".<br/>";
        echo "<br/>";
        $selected_meal_price = $steak_price;
        $selected_drink_price = $steak_drink_price;
    }
    else if ($dinner == "2") {    
        echo "A glass of " . $salmon_drink . " for a luxuriously priced $" .  $salmon_drink_price . " is a wonderful complement to our salmon<br/>"; 
        echo "<br/>";
        $selected_meal_price = $salmon_price;
        $selected_drink_price = $salmon_drink_price;
    }
    else if ($dinner == "3") { 
        echo "Try a pint of " . $barbecue_pork_drink . " for only $" . $barbecue_pork_drink_price . ".<br/>"; 
        echo "<br/>";
        $selected_meal_price = $barbecue_pork_price;
        $selected_drink_price = $barbecue_pork_drink_price;
    }
    else if ($dinner == "4") { 
        echo "Stiller and Meara invite you to try " . $chicken_drink . " at $" . $chicken_drink_price . " per glass with the chicken!.<br/>"; 
        echo "<br/>";
        $selected_meal_price = $chicken_price;
        $selected_drink_price = $chicken_drink_price;
    }
    else {
        echo "Please select an entree from our drop-down menu and we will recommend a beverage suited to your choice.";
        echo "<br/>";
    }

    if(!is_null($selected_meal_price) && !is_null($selected_drink_price)) {
        $receipt = wallet_buster($selected_meal_price, $selected_drink_price);
    }

    return $receipt;
}

【讨论】:

    【解决方案3】:

    正如其他人所指出的那样,您几乎在那里。您可能还需要注意,如果您使用双引号字符串 (") 而不是单引号 ('),php 将解释其中的变量。

    $var = 3;
    echo "The value of var is $var"; //The value of var is 3
    

    当然,当您需要实际的美元符号 (\$) 时,您还需要记住将 $ 转义。您还可以考虑使用sprintf 使您的输出更干净。

    另一个有用的提示是进入终端并运行php -l &lt;file&gt; 来检查语法错误。

    <?php
    
    echo "<h3>Thank you for dining at Elysium Excelsior</h3><br>";
    
    function wallet_buster($entree_price, $drink_price) {
        $taxed_cost = 1.1 * ($entree_price + $drink_price);
        echo "<br/>";
    
        return $taxed_cost; 
    }
    
    function select_beverage($dinner) {
        $steak_price = 27.50;
        $steak_drink = "Justin Cabernet Sauvignon"; 
        $steak_drink_price = 13.15; 
    
        $salmon_price = 24.95;
        $salmon_drink = "Russian River Pinot Noir"; 
        $salmon_drink_price = 12.25; 
    
        $barbecue_pork_price = 22.99;
        $barbecue_pork_drink = "Dogfish Head 120 Minute IPA";
        $barbecue_pork_drink_price = 7.99;
    
        $chicken_price = 21.50;
        $chicken_drink = "Blue Nun Sauvignon Blanc";
        $chicken_drink_price = 12.25; 
    
        if ($dinner == "1") { 
            echo "The filet mignon pairs wonderfully with a glass of $steak_drink at a price of \$ $steak_drink_price .<br/>";
            echo "<br/>";
            $receipt = wallet_buster($steak_price, $steak_drink_price);
        }
        else if ($dinner == "2") { 
            echo "A glass of $salmon_drink for a luxuriously priced \$ $salmon_drink_price is a wonderful complement to our salmon.<br/>"; 
            echo "<br/>";
            $receipt = wallet_buster($steak_price, $steak_drink_price); 
        }
        else if ($dinner == "3") { 
            echo "Try a pint of $barbecue_pork_drink for only \$ $barbecue_pork_drink_price.<br/>"; 
            echo "<br/>";
            $receipt = wallet_buster($steak_price, $steak_drink_price);
        }
        else if ($dinner == "4") { 
            echo "Stiller and Meara invite you to try $chicken_drink at \$ $chicken_drink_price per glass with the chicken!<br/>"; 
            echo "<br/>";
            $receipt = wallet_buster($steak_price, $steak_drink_price);
        }
        else {
            echo "Please select an entree from our drop-down menu and we will recommend a beverage suited to your choice.";
            echo "<br/>";
        }
    
        return $receipt;
    
    }
    
    $dinner = $_GET["entree"];
    
    $big_deal_meal = select_beverage($dinner);
    
    echo "<br>";
    echo "We encourage our patrons to tip well; given your menu selections, we` `believe your bill should be : $" . (1.25 * $big_deal_meal);
    
    ?>
    

    祝你在 O'Reilly 课程的其余部分好运。

    【讨论】:

    • 鹰眼火爆!是的,我正在学习 OST 的 PHP 入门课程。我的导师很棒,但是当代码提交和响应之间有几天的时间差时,沟通可能会令人沮丧。大多数 OST 的教练都很棒,但我觉得他们忙于工作。感谢您的 cmets 和指向 sprintf 的指针。
    猜你喜欢
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    相关资源
    最近更新 更多