Table of Contents
PHP Addition of two numbers: PHP program
In this article, we will discuss the concept of PHP Addition of two numbers
In this post, we are going to learn how to write a program to find addition of two numbers in PHP language
Code to sum of two numbers in PHP
Addition of two numbers in PHP
In this code, we are going to learn how to write a program to addition of two numbers in PHP programming language
Program 1
- Start PHP script
- Declare and initiates two integer variable
- Calculate sum of given two numbers
- Display result on the screen
- Exit PHP
In this program, the user initiates the values to variables and calculates the sum of the given values using plus(+) operator
<?php $num1=5; $num2=10; $sum=$num1+$num2; echo "Sum: ",$sum; ?>
When the above code is executed it produces the following result
Sum: 15
Division of two numbers in PHP using third variable
In this program, the user initiates the values to variables and calculates the sum off the given numbers using plus(+) operator .Then the result is assigned to third variable (msg) .finally the result is displayed on the screen.
Program 2
- Start PHP script
- Declare and initiates two integer variable
- Calculate sum of given two numbers
- Assign the result to third variable
- Display result on the screen
<?php $x=5; $y=10; $sum=$x+$y; $msg = 'Sum of x and y:' ; print($msg.$sum); ?>
When the above code is executed it produces the following result
Sum of x+y = 35
Division of two numbers in PHP -using form
In this program, the user is entered the values to variables using form(the program Asks input from the user to enter the form) and calculates the sum off the given numbers using plus(+) operator .Then the result is assigned to third variable (sum) .finally the result is displayed on the screen.
- Create HTML form
- Exit HTML form
- Start PHP script
- Exit PHP
Program 3
<html> <head> <title>Sum of given two numbers </title> </head> <body> <h3>Calculate addition of given numbers</h3> <form method="post"> Enter 1st value: <input type="number" name="number1" /><br><br> Enter 2nd value: <input type="number" name="number2" /><br><br> <input type="submit" name="submit" value="Add"> </form> <?php if(isset($_POST['submit'])) { $number1 = $_POST['number1']; $number2 = $_POST['number2']; $sum = $number1+$number2; echo "The sum of $number1 and $number2 is: ".$sum; } ?> </body> </html>
When the above code is executed it produces the following result
Division of two numbers in PHP -without + operator
In this program, the user is entered the values to variables (the program Asks input from the user) and calculates the sum off the given values using without plus(+) operator .And then the result is displayed on the screen.
- Create HTML form
- Exit HTML form
- Start PHP script
- Exit PHP
Program 4
<html> <head> <title>Addition of given two numbers </title> </head> <body> <h3>Calculate addition of two numbers </h3> <form> <b>Enter 1st number for sum:</b> <input type="number" name="number1" /> <br> <br> <b>Enter 2nd number for sum: </b> <input type="number" name="number2" /> </br> <input type="submit" name="submit" value="Sum"> </form> </body> <?php @$number1 = $_GET['number1']; @$number2 = $_GET['number2']; for($i=1; $i<=$number2; $i++) { $number1++; } echo "Sum of given two numbers".$number1 ; ?>
When the above code is executed, it produces the following result.
Enter number to form
click the sum button to find addition
Suggested for you
Java program to add two numbers
C++ program to add two numbers
Python program to add two numbers
Write a program to sum of prime numbers 1 to n in Java language
Write a program to sum of prime numbers 1 to n in C language
Write a program to sum of prime numbers 1 to n in C++ language
Write a program to sum of prime numbers 1 to n in Python language
Write a program to sum of natural numbers 1 to n in Java language
Write a program to sum of prime numbers 1 to n in C language
Write a program to sum of prime numbers 1 to n in C++ language
Write a program to sum of prime numbers 1 to n in Python language