Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
PHP subtraction of two numbers-PHP program

PHP subtraction of two numbers-PHP program

Posted on July 7, 2022July 18, 2022

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
    • Find subtraction of two numbers  – #2
    • Find subtraction of two numbers  using form
    • Find subtraction of two numbers without plus(+) operator
  • Related

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 in C#

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

Addition two number  in C++ language

Addition two number in Python language

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes