Site icon T4Tutorials.com

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 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>
    <title>Without_Form</title>
</head>
<body>
    <h1 align="center">Without Form Sum</h1>
    <br />
    <form 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

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

Download database file: addition

 

<?php declare(strict_types=1);  
function T4Tutorials_sum(int $a, int $b) {
  $t = $a + $b;
  return $t;
}

echo "5 + 10 = " . T4Tutorials_sum(5, 10) . "<br>";
echo "7 + 13 = " . T4Tutorials_sum(7, 13) . "<br>";
echo "2 + 4 = " . T4Tutorials_sum(2, 4);
?>

Read about PHP Sum Of Array Elements

How to write simple SQL queries for addition using PHP?

SELECT SUM(Quantity) AS sold FROM Orders;

This SQL Query return the sum of the “Quantity” field in the “Orders” table.

PHP Add two textbox values and display  in third textbox

<?php
extract($_POST);
// store the result in $third
if(isset($add))
{
$third=$first+$second;
}	
 
?>

<html>
<head>
	<title>add two text boxes and Display the result in 3rd text box with PHP</title>
</head>
<body>
<form method="post">
 <table align="center" border="1">
	<Tr>
	<th>Your Result</th>
	<td><input type="text" readonly="readonly" value="<?php echo @$third;?>"/></td>
	</tr>
	<tr>
		<th>Enter first number</th>
		<td><input type="text" name="$first" value="<?php echo @$first;?>"/></td>
	</tr>	
	<tr>
		<th>Enter second number</th>
		<td><input type="text" name="snum" value="<?php echo @$second;?>"/></td>
	</tr>
	<tr>
		<td align="center" colspan="2">
		<input type="submit" value="+" name="add"/>
		
	</tr>
	</table>	
</form>
</body>
</html>

Frequently Asked Questions (FAQ)

______ are used in PHP to perform mathematical calculations, such as addition.

(A). Bitwise operators
(B). Binary operators
(C). Unary operators
(D). Arithmetic operators

Answer - Click Here:
(D). Arithmetic operators

The order of precedence for arithmetic expressions causes

(A). increment operations to be done first
(B). multiplication to be done before division
(C). addition to be done before subtraction
(D). modulus operations to be done last

Answer - Click Here:
(C). addition to be done before subtraction

Exit mobile version