PHP subtraction of two numbers
PHP subtraction of two numbers-PHP program
In this article, we will discuss the concept of PHP subtraction of two numbers-PHP program
In this post, we are going to learn how to write a program to find difference of two numbers in PHP language
Table of Contents
Program 1
In this program, the user initiates the values to variables and calculates the difference of the given numbers using minus(-) operator. And then the result is displayed on the screen.
Program 1
<html> <head> <title> subtraction of two numbers</title> </head> <body> <h3>Subtract two numbers</h3> <?php $num1=40; $num2=15; $sum=$num1-$num2; echo "Difference: ".$sum; ?> </body> </html>
When the above code is executed, it produces the following result
Subtract two numbers
Difference: 25
In this program, the user initiates the values to variables and calculates the difference of the given numbers using minus(-) operator. then the result is assigned to another variable ($msg) and the the result is displayed using that variable
Program 2
<html> <head> <title> Subtraction of two numbers</title> </head> <body> <h3>Subtract two numbers</h3> <?php $num1=300; $num2=150; $sum=$num1-$num2; $msg= "Difference: ".$sum; echo $msg; ?> </body> </html>
When the above code is executed, it produces the following result
Subtract two numbers
Difference: 150
Program 3
In this program, the user is entered the values to variables using form(the program Asks input from the user to form) and calculates the difference of the given numbers using minus(-) operator .Then the result is assigned to third variable ($sub) .finally the result is displayed on the screen using third variable.
<html>
<head>
<title> Subtraction of two numbers</title>
</head>
<body>
<h3>Subtract two number using form</h3>
<form method = "post">
Enter 1st number
<input type = "number" name="number1"/><br><br>
Enter 2nd number
<input type = "number" name="number2"/><br><br>
<input type = "submit" name="submit" value="Sub"/><br>
</form>
<?php
if(isset($_POST['submit']))
{
$number1=$_POST['number1'];
$number2=$_POST['number2'];
$sub=$number1 - $number2;
echo "The Difference of $number1 and $number2 is: ".$sub;
}
?>
</body>
</html>
When the above code is executed, it produces the following result
Program 4
In this program, the user is entered the values to variables using form(the program Asks input from the user to form) and calculates the difference of the given numbers using without minus(-) operator .Then the result is assigned to third variable ($sub) .finally the result is displayed on the screen.
<html>
<head>
<title> Subtraction of two numbers</title>
</head>
<body>
<form>
<h3>Subtract two number using form</h3>
Enter 1st number
<input type = "number" name="number1"/><br><br>
Enter 2nd number
<input type = "number" name="number2"/><br><br>
<input type = "submit" name="submit" value="Sub"/><br>
</form>
</body>
<?php
@$number1=$_GET['number1'];
@$number2=$_GET['number2'];
for($i=1; $i<=$number2; $i++){
$number1--;
}
echo "Difference is: ".$number1;
?>
</html> When the above code is executed, it produces the following result
Suggested post
Subtract two number in Java language
Subtract two number in C language
Subtract two number in C++ language
Subtract two number in Python language
Sum of two integers using function in C#
Sum of two floating point numbers in C#
Addition two number in Java language
Addition two number in C 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.