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
Code to difference of two numbers in PHP
Find subtraction of two numbers – #1
Program 1
- Start HTML tag
- Start PHP script
- Declare and initiates two integer variable
- Calculate difference of given two numbers
- Display result on the screen
- Exit PHP
- Close HTML tag
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
Find subtraction of two numbers – #2
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
Find subtraction of two numbers using form
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.
- Create HTML form
- Exit HTML form
- Start PHP script
- Exit PHP
<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
Find subtraction of two numbers without plus(+) operator
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.
- Create HTML form
- Exit HTML form
- Start PHP script
- Exit PHP
<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