PHP subtracting two numbers-PHP program
- Home
- Calculations
- PHP subtracting two numbers-PHP program
- On
- By
- 0 Comment
- Categories: Calculations, subtraction
PHP subtracting two numbers-PHP program
PHP subtracting two numbers-PHP program
In this tutorial, we will discuss the concept of PHP subtracting two numbers-PHP program
In this topic, we are going to learn how to write a program to subtract two numbers in PHP programming language
Find Difference of two numbers
Find difference of two numbers – #1
In this program, the user declare two integer variables $x and $y. Then two numbers are subtracted using minus(-) operator. finally the result is displayed on the screen
Program 1
<?php $num1=30; $num2=15; $sum=$num1-$num2; echo "Difference: ".$sum; ?>
When the above code is executed, it produces the following result
Difference: 15
Find difference of two numbers – entered by user
In this program, the user asked to enter two numbers(by passing input value to form). Then two numbers are subtracted using + operator and the result is displayed on the screen
Program 2
<html> <body> <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 difference of two numbers – without + operator
In this program, the user asked to enter two numbers(by passing input value to form). Then two numbers are added using without + operator and the result is displayed on the screen
Program 3
<body> <form> 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; ?>
When the above code is executed, it produces the following result
Suggested for you
Subtraction of two numbers in Java language
Subtraction of two numbers in C Language
Subtraction of two numbers in C++ Language
Subtraction of two numbers in Python Language
Subtraction of two numbers using method in Java language
Subtraction of two numbers using function in C Language
Subtraction of two numbers using function in C++ Language
Subtraction of two numbers using function in Python Language