PHP Adding Two Numbers  – PHP program

PHP Adding Two Numbers  – PHP program

In this tutorial, we will discuss the PHP Adding Two Numbers  – PHP program

In this topic, we are going to learn how  to write a program to add two numbers in PHP programming language

Java program to sum of two numbers
Sum of two numbers

Sum of two numbers

Sum of two numbers using variables

Program 1

Addition of two numbers 25 and 50 and the output is displayed here

<?php
$n1=25;
$n2=50;
$sum=$n1+$n2;
    echo "Sum: ",$sum;
?>

When the above code is executed, it produces the following result

Sum: 75

Sum of two numbers by passing input to form

Program 2

In this program, the user is asked to enter two numbers(by passing input value to form).  Then two numbers are added using the + operator and the result is displayed on the  screen

<html>
<head>
  <title>Addition of two numbers </title>
  </head>
//create html from
<form method="post">  
Enter First Number:  
<input type="number" name="number1" /><br><br>  
Enter Second Number:  
<input type="number" name="number2" /><br><br>  
<input  type="submit" name="submit" value="Sum">  
</form> 
//End HTML form 

// php script
<?php  
    if(isset($_POST['submit']))  
    {  
        $number1 = $_POST['number1'];  
        $number2 = $_POST['number2'];  
        $sum =  $number1+$number2;  
//calculate  sum of two numbers using + operator
echo "The sum of $number1 and $number2 is: ".$sum;   
//display sum of two numbers
}  
?>  
//Exit
</body>
</html>


When the above code is executed, it produces the following result

PHP Adding Two Numbers - PHP program
Adding Two Numbers

Sum of two numbers by passing input to form without + operator

Program 3

In this program, the user is asked to enter two numbers(by passing input value to form).  Then two numbers are added without using + operator and the result is displayed on the  screen

<html>
  <body>
<form>  //Start html form
Enter 1st number for sum:  
<input type="number" name="number1" /><br><br>  
Enter 2nd number for sum:  
<input type="number" name="number2" /><br><br>    
<input  type="submit" name="submit" value="Sum">  
</form>  //End form
</body>
<?php  //PHP script
  @$number1 = $_GET['number1'];  
  @$number2 = $_GET['number2'];  
  for($i=1; $i<=$number2; $i++) 
  {//calculate sum of two numbers using for loop
  $number1++;
        }			
echo "Sum of given two numbers".$number1
//display sum of two numbers
;  
//Exit   
?>  

</html>

When the above code is executed, it produces the following result

PHP Adding Two Numbers - PHP program
Adding Two Numbers

Similar post

Sum of two numbers in C language

Sum of two numbers in C++ language

Sum of two numbers in Python language

Sum of two numbers in Java language without + operator

Sum of two numbers in Java language using bitwise operator

Sum of two numbers in C language without + operator

Sum of two numbers in C language using bitwise operator

 

Calculate addition of two numbers using method in Java language

Calculate addition of two numbers using function in C language

Calculate addition of two numbers using function in C++ language

Calculate addition of two numbers using function in Python language

 

Operator in Java language

Operator in C language

Operator in C++ language

Operator in Python language

By Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Leave a comment

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