PHP Program to Add Two Numbers Very Easily

Hello Learners, It sounds quite easy to add two numbers in PHP right? PHP program to add two numbers is definitely very easy. As it does not require a bunch of knowledge. But yes this program will help you to learn more several things you can do with PHP.

In this tutorial, I will cover up the below following things

  • PHP Program to add two numbers with core PHP
  • Add two numbers with HTML form in PHP
  • Add two numbers without using the + operator in PHP

PHP Program to add two numbers with Core PHP

The below code will display the sum of two numbers:

<?php
  $a=5;
  $b=6;
  $c=$a+$b;
  echo "Sum is: $c";

?>

OUTPUT:

Sum is: 11

Explanation: There are two variables $a and $b. What we did is just took another variable $c and assigned its value with the summation of $a and $b.

Then just simply echo out this Sum is: $c

As $c is now assigned with $a+$b so it will display 11.

Also Read,

Reverse A Number in PHP with Various Methods

PHP Program To Find The Largest Number of Given Values

Add two numbers in PHP using HTML form

Here we gonna create a PHP file where the user can input the value of two numbers in an HTML form. When the user will press the submit button or hit enter the PHP script will process the request and echo out the summation of the two numbers. Below is the code to do that

Please Note That: 

<form action="index.php" method="post">

I have used index.php in form action field as The PHP file is named as index.php. You have to use your file name whatever you have given to create your PHP file.
And one more thing is that you can use GET method if you want.

<!DOCTYPE html>
<html>
<head>
  <title>Summation Of two numbers using PHP</title>
</head>
<body>
  <form action="index.php" method="post">
    <input type="numbers" name="num1">
    <input type="numbers" name="num2">
    <input type="submit" name="submit">
  </form>

</body>
</html>
<?php
if (isset($_POST['num1']) && isset($_POST['num2'])) {
  $result=$_POST['num1'] + $_POST['num2'];
  echo $result;
  
}
  
?>

If you run this PHP file on your server you will get two text boxes. Enter two numbers and click on submit. The result will be displayed.

Add two numbers without using the + operator in PHP

<!DOCTYPE html>
<html>
<head>
  <title>Summation Of two numbers using PHP</title>
</head>
<body>
  <form action="index.php" method="post">
    <input type="numbers" name="num1">
    <input type="numbers" name="num2">
    <input type="submit" name="submit">
  </form>

</body>
</html>
<?php
if (isset($_POST['num1']) && isset($_POST['num2'])) {
  $a=$_POST['num1'];
  $b=$_POST['num2'];
  for($i=0;$i<$a;$i++){
    $b++;
  }
  echo $b;
}
  
?>

Echo and Print Statement Difference in PHP

3 responses to “PHP Program to Add Two Numbers Very Easily”

  1. Abu Sayed Mondal says:

    hopefully I can say that this website is better than any other website ,thanks for this type of website which I was finding … We all the visitor should use this website ,,,thanks again

  2. Saruque Ahamed Mollick says:

    Thank you very much for your support towards us.

  3. upssses says:

    How do I move a thread to a different topic?
    hi all 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *