In this example, I’ll show you how to add two numbers entered by user in PHP
Output:
PHP Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php $sum=0; $num1=""; $num2=""; if(isset($_POST["add"])) { $num1= $_POST["num1"]; $num2= $_POST["num2"]; $sum = $num1 + $num2; } ?> <!doctype html> <html> <head> <title>Code4Examples</title> <meta charset="utf-8"> <style> label{ display: block; } </style> </head> <body> <h1><?=$sum?></h1> <form action="" method="post"> <label for="num1">Number 1: <input type="text" name="num1" value="<?=$num1?>"> </label> <label for="num2">Number 2: <input type="text" name="num2" value="<?=$num2?>"> </label> <button name="add" type="submit">Add Numbers</button> </form> </body> </html> |
[…] Add Two Numbers in PHP […]