PHP Addition of two numbers:
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.