【发布时间】: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!"".<br/>";应该是. " per glass with the chicken!<br/>";和$steak_drink . at a price of $"应该是$steak_drink . "at a price of $" -
作为一个葡萄酒势利小人,我想指出黑比诺配鲑鱼是一个糟糕的选择。