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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂