Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Program to add two numbers in PHP and show their sum with form and with database

Program to add two numbers in PHP and show their sum with form and with database

In this tutorial, we will learn about the followings.

  1. Program to add two numbers in PHP 
  2. Program to add two numbers in PHP  with form
  3. Program to add two numbers in PHP  with database

Program to add two numbers in PHP.

In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2. 

 <?php 
 $a=4;
 $b=9;
 $sum=$a+$b;
 echo $sum;
 ?>

Program to add two numbers in PHP  with form.

In this example, we will take the inputs from the user and store them in variables.

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <h4><b>Enter 1st Value</b></h4>
    
       <input type="text" name="one" id="one" class="form-control" placeholder="1st Digit">
       <br >
    <h4><b>Enter 2nd Value </b></h4>
    
       <input type="text" name="two" id="two" class="form-control" placeholder="2nd Digit">
       <br /><br />
       <input type="submit" name="submit">
    </form>
      
      <h4><b>Result:</b></h4>  
      <br >
        <?php 
        if(isset($_POST['submit'])){
            $sum=0;
            $one=$_POST['one'];
            $two=$_POST['two'];
              //echo $one;
            $sum=$one+$two;
            echo $sum;
            exit;
            }
      
             ?>

Program to add two numbers in PHP  with a database.

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="addition";
// Create connection
$con = mysqli_connect($servername, $username, $password, $db);
// Check connection
if (mysqli_connect_error()) {
    echo "Connection failed: " .mysqli_connect_error();
}

?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Without_Form</title>
</head>
<body>
    <h1 align="center">Without Form Sum</h1>
    <br />
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <h4><b>Enter 1st Value</b></h4>
    
       <input type="text" name="one" id="one" class="form-control" placeholder="1st Digit">
       <br >
    <h4><b>Enter 2nd Value </b></h4>
    
       <input type="text" name="two" id="two" class="form-control" placeholder="2nd Digit">
       <br /><br />
       <input type="submit" name="submit" value="Add & Save">
    </form>
      <br /><br /><br /><br />
      <h4><b>Result:</b></h4>  
      <br >
        <?php 
        if(isset($_POST['submit'])){
            $sum=0;
            $one=$_POST['one'];
            $two=$_POST['two'];
              //echo $one;
            $sum=$one+$two;
            echo $sum;
            mysqli_select_db($con,"addition");
            $insert="INSERT INTO sum_table (value1,value2,result) VALUES ('$one','$two','$sum')";
            if (mysqli_query($con,$insert)){
               echo "<br />";
               echo "<br />";
               echo "DATA IS INERTED ALSO SUCCESSFULLY."; 
                
            }
            else{
                echo "SOME ERROR".mysqli_error($con);
            }
            //exit;
            }
      
             ?>
</body>
</html>

Database of Program to add two numbers in PHP

Program to add two numbers in PHP and show their sum with form and with databases

Figure: Program to add two numbers in PHP and show their sum with form and with databases.

 

Prof. Fzal Rehman Shamil
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325