我是一个试图用PHP编写简单程序的绿色新手。我使用一个HTML表单来请求一个“小餐馆”选择一个主菜单,它将选择发送给一个PHP程序.PHP程序应该与主菜单选项相呼应,建议小餐馆喝一杯,然后告诉小餐馆什么主菜的成本,饮料是 - 包括税和小费。如何从PHP中的第二个函数返回值?
第一个函数select_beverage接受主菜单的选择并回应建议的饮料和饮料价格。然后它调用函数wallet_buster来计算账单的纳税成本。 Wallet_buster()
然后应该将征税成本返还给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);
?>
这个程序在做什么并不是预期或期望的行为?只需要经历所有代码来发现问题就很困难 - 如果你能帮助我们解决你遇到的确切问题(某个变量不能正确打印等),那么它将是一个巨大的帮帮我。 – lmcphers
您可以看到基于颜色编码的语法错误。 –
您可能想要在四种情况下分别传递不同的变量: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种情况都是一样的)。但也许你的问题是另一回事......? – ippi