Table of Contents
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
In this code, we are going to learn how to write a program to addition of two numbers in PHP programming language
Program 1
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
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
<?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
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.
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
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.
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.