du7979 2015-06-17 02:49
浏览 67
已采纳

如何从PHP中的第二个函数返回值?

I'm a green green newbie trying to write a simple program in PHP. I use an HTML form to ask a "diner" to select an entree, which sends the choice to a PHP program.The PHP program is supposed to echo the entree choice, suggest a drink to the diner, and then tell the diner what the cost of the entree, drink is--including tax and tip.

The first function select_beverage accepts the choice of entree and echoes out the suggested drink and drink price. It then calls function wallet_buster which calculates the taxed cost of the bill. Wallet_buster() is then supposed to return the taxed cost back to select_beverage() which in turn should return the taxed cost back to the variable that called select_beverage.

I can get a simplified version of this program to work but not this beast. My teacher suggested that I save the value returned from wallet_buster as a variable, which I would then return at the end of the if/else cascade. I've tried to follow that suggestion in this code but it's not working. I've also tried

return wallet_buster($steak_price, $steak_drink_price);

in each if/else function but that's not working either.

Thanks in advance for any enlightenment you can provide!

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

?>
  • 写回答

3条回答 默认 最新

  • doujian4752 2015-06-17 03:25
    关注

    Your code is mostly working, you simply misplaced some quotes and concatenation. Adding quotes where they shouldn't be or forgetting to add them where needed will cause PHP to misinterpret your code. You might consider using a code editor or IDE to avoid this in the future. They will highlight your code to alert you when you make a mistake like missing a quote. An IDE like Netbeans will constantly check your code for syntactical mistakes.

    Enabling error reporting in your PHP configuration will also give you useful hints about what is going wrong in your scripts.

    Here is the working code:

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

    Also note that you do not need to concatenate (with a '.') the text and html. Only when you're mixing in PHP code like variables.

    So this: "<span>" . "I am a string" . "</span><br>"; is unnecessary. This: "<span>I am a string</span><br>"; is just fine and easier to read.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c