Site icon TestingDocs.com

PHP Program to Add Two Numbers

PHP Program to Add Two Numbers

In this post, we will learn how to add two numbers using PHP. Sample PHP Program is the given below to get the sum of the two numbers a and b.

PHP Project

I would be using Eclipse with PDT installed. Create a New PHP Project in Eclipse.

New >> PHP Project.

 

PHP file

Create a new PHP file under the project created.

New >> New PHP file.

 

Add 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>

 

 

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 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:

https://www.testingdocs.com/php-tutorials/

Exit mobile version