我是一个绿色的新手试图用PHP编写一个简单的程序。我使用HTML表单来询问"晚餐"选择一个主菜,将选择发送到PHP程序.PHP程序应该回应主菜选择,建议给餐馆喝一杯,然后告诉用餐者主菜,饮料的成本是多少 - 包括税和提示。
第一个功能select_beverage接受主菜的选择,并回应建议的饮料和饮料价格。然后调用函数wallet_buster来计算账单的纳税成本。然后,Wallet_buster()
应该将纳税成本返回select_beverage()
,而if/else
又应将纳税成本返回给调用select_beverage的变量。
我可以得到这个程序的简化版本,但不是这个野兽。我的老师建议我将wallet_buster返回的值保存为变量,然后我会在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);
?>
函数中,但这两个函数都不起作用。
提前感谢您提供的任何启示!
wchar_t
答案 0 :(得分:0)
这样的东西?
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;
}
答案 1 :(得分:0)
你的代码大部分都在工作,你只是放错了一些引号和连接。在不需要添加引号的地方添加引号会导致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);
?>
另请注意,您无需连接(使用&#39;。&#39;)文本和html。只有当你在PHP代码中混合变量时才会这样。
所以:"<span>" . "I am a string" . "</span><br>";
是不必要的。这个:"<span>I am a string</span><br>";
很好,更容易阅读。
答案 2 :(得分:0)
正如其他人所指出的那样,你几乎就在那里。您可能还需要注意,如果您使用双引号字符串(“)而不是单引号('),则php将解释其中的变量。
$var = 3;
echo "The value of var is $var"; //The value of var is 3
当然,当你需要实际的美元符号(\ $)时,你还需要记住逃避$ for。您还可以考虑使用sprintf使输出更清晰。
另一个有用的提示是进入终端并运行php -l <file>
以检查语法错误。
<?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课程的其余部分好运。