settingsAccountsettings
Menusettings

Q: Sum two (2) numbers in PHP with HTML input form

+2 votes
  1. Write a PHP script to sum two numbers.
  2. The script should display HTML form to enter the numbers.
  3. If the numbers are passed as parameters, display their sum.
asked in PHP category by user john7

1 Answer

+1 vote
 
Best answer

Here's my solution:

<!DOCTYPE html>
<html>
<head>
    <title>Sum</title>
</head>
<body>

<form>
    <div>Number 1:</div>
    <input type="number" name="num1"/>
    <div>Number 2:</div>
    <input type="number" name="num2"/>
    <div><br><input type="submit" value="CHECK THE SUM"></div><br>
</form>

<?php
if (isset($_GET['num1']) && isset($_GET['num2'])) {

    $num1 = $_GET['num1'];
    $num2 = $_GET['num2'];
    $sum = $num1 + $num2;
    echo $num1 . " + " . $num2 . " = " . $sum;
}
?>

</body>
</html>

 

answered by user Jolie Ann
selected by user golearnweb
...