我正在尋找一種合適的方式來計算用戶提交的數字,以便通過PHP進行100%的計算並進行一些基本驗證。如何使用php從表單輸入中計算100%(百分比)
用戶應該能夠輸入數字從1到100,我正在尋找正確的方式來計算總和,例如對於num1
25和50的num2
將等於75%,我試圖通過這樣的形式實現這一目標:
<form>
<input type="number" name="num1" min="1" max="100">
<input type="number" name="num1" min="1" max="100">
<input type="submit" name="send" value="Calculate">
</form>
,使用PHP,我想這樣做:
<?php
// Check if form was sent:
if(isset($_POST['send'])){
// Set variables with numbers equal to 0:
$num1 = 0;
$num2 = 0;
// Update the value by the form submitted data:
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
// Calculation:
// Check if sum is not more than 100%
if($num1+$num2 > 100) {
echo "Error: more than 100%";
}
if($num1+$num2 < 0.1) {
echo "Error: less than 1%";
}
?>
我應該使用這一些先進的方法是什麼?有沒有更適合的方式來使用PHP?
編輯
高級?比求和更高級? –
'if($ num1 + $ num2 <0.1)'應該是'if($ num1 + $ num2 <1)',但除此之外,我不知道你在努力達到什麼目的。 – Flosculus
你想實現什麼 –