- On
- By
- 0 Comment
- Categories: addition, Calculations
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 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
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
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