addition

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

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

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

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

How to divide two floating-point number using function in C#
PHP program to Addition of Two Numbers using function - PHP program
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.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago