PHP Program to Add Two Numbers
PHP Program to Add Two Numbers
In this post, you will learn how to add two numbers using PHP. A sample PHP Program is given below to get the sum of the two numbers a and b.
Create New PHP Project
I would be using Eclipse with PDT installed. Create a New PHP Project in Eclipse.
New >> PHP Project.

Create a New PHP file
Create a new PHP file under the project created.
New >> New PHP file.

Add Two numbers PHP Code
Add PHP code to the file as shown below:
<html>
<head>
<title>Addition of Numbers</title>
</head>
<body>
<form action="" method="post">
<label>Enter a:</label>
<input type="text" name="a" />
<br>
<br>
<label>Enter b:</label>
<input type="text" name="b" />
<br>
<br>
<input type="submit" name="Sum" value="Add a+b">
</form>
<?php
if(isset($_POST['Sum']))
{
$a = $_POST['a'];
$b = $_POST['b'];
$total = $a +$b;
echo "The value of a + b=".$total;
}
?>
</body>
</html>
PHP Program Screenshot

PHP Program
Save the changes and copy the file to Apache htdocs. You can also link the server in Eclipse. You can copy the file under a directory called first program. So that you can access the file with below URL.
http://localhost/firstprogram/AddNumbers.php
Start the Apache server and access the url from the browser as shown below.

Run the PHP Program
Run the program. Enter sample values for a and b.

Click on the button. The result of the sum would be displayed.

PHP Tutorials
PHP Tutorials on this website: