2015-06-17 39 views
0

我是一个试图用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); 

?> 
+0

这个程序在做什么并不是预期或期望的行为?只需要经历所有代码来发现问题就很困难 - 如果你能帮助我们解决你遇到的确切问题(某个变量不能正确打印等),那么它将是一个巨大的帮帮我。 – lmcphers

+1

您可以看到基于颜色编码的语法错误。 –

+0

您可能想要在四种情况下分别传递不同的变量: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

回答

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); 
?> 

还要注意的是,你不需要来串联文本和HTML(用“”)。只有当你像变量一样在PHP代码中混合时。

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

+0

感谢关于IDEs的建议!顺便说一句,我们都错过了,我正在通过牛排晚餐的论点为每一种晚餐。 – Gondar

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; 
} 
0

正如其他人指出的那样,你几乎就在那里。您可能还需要注意的是,如果你有一个双引号字符串(“),而不是单引号('),PHP将在解释变量。

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

当然,你还需要记住逃脱$用于当你需要实际的美元符号(\ $)。你也可以看看使用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课程的其余部分。

+0

Eagle-eyed pyriccrash!是的,我正在通过OST的PHP入门介绍。我的导师很棒,但是可能会让人感到沮丧代码提交和响应之间有几天的时间滞后。大部分OST的导师都很棒,但我的印象是他们工作潦倒。感谢您的意见和指向sprintf的指针。 – Gondar

相关问题