繁体   English   中英

PHP-询问用户输入-计算

[英]PHP- Asking the User Input- Calculating

我一直在尝试使这个简单的程序起作用,但是由于某种原因,我没有得到任何结果。 任何建议将不胜感激。 谢谢。

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    Price of the Meal
    <input value="100" type="text" name="meal"><br>
    Tip
    <input value="20" type="text" name="tip">
    <input type="submit" name="submit">


    <?php
    $meal = $_POST['meal'];
    $tip = $_POST['tip'];

    $total_amount = check($meal, 10, $tip);

    function check($meal, $tax, $tip) {

        $tax_amount = $meal * .10;

        $tip_amount = $meal * ($tip / 100);

        $total_amount = $meal + $tax_amount + $tip_amount;
        echo "Price of the meal " . $meal . "\n";


        echo "tax_amount " . $tax_amount . "\n";
        echo "tip_amount " . $tip_amount . "\n";

        return $total_amount;
    }

    echo "Total " . $total_amount;
    ?>

代码几乎没有变化

$tax_amount = $meal * ($tax/100);

并添加条件

 if (isset($_POST['meal']) && isset($_POST['tip']) && $_POST['meal'] > 0 && $_POST['tip'] > 0) {
  echo "Price of the meal " . $meal . "\n";
  echo "tax_amount " . $tax_amount . "\n";
  echo "tip_amount " . $tip_amount . "\n";
}


if (isset($_POST['meal']) && isset($_POST['tip']) && $_POST['meal'] > 0 && $_POST['tip'] > 0)
  echo "Total " . $total_amount;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM