簡體   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