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
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
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
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…