Table of Contents
PHP program to subtract two numbers using function
In this article, we will discuss the concept of PHP program to subtract two numbers using function
In this post, we are going to learn how to write a program to find subtraction of two numbers using function in PHP language
Code to difference of two numbers in PHP
Subtraction of two numbers using function – #1
Program 1
- Start PHP script
- Declare two integer variable as parameter
- Calculate difference of given two numbers and return value
- Calling the function with argument
- Display result on the screen
- Exit PHP
In this program, first the user declare variables as parameter in function. Then when the user call the function, passing the values to variables as argument. finally , the result is calculated and display on the screen
<html> <head> <title> Subtraction of two numbers</title> </head> <body> <?php //function definition function subTwoNumbers($x, $y) { return $x - $y; } //Calling the function and print result echo "Difference of 200 and 30: ".subTwoNumbers(200,30); echo "<br>"; echo "Difference of 150 and 550: ".subTwoNumbers(150,550); echo "<br>"; echo "Difference of 100 and -40: ".subTwoNumbers(100,-40); echo "<br>"; ?> </body> </html>
When the above code is executed, it produces the following result
Difference of 200 and 30: 170 Difference of 150 and 550: -400 Difference of 100 and -40: 140
Subtraction of two numbers using function – #2
In this code, we are going to learn how to write a program to calculate difference of two numbers using function in PHP programming language
Program 1
- Start PHP script
- Declare and initiates two integer variable as parameter
- Calculate difference of given two numbers and return value
- Calling the function and Display result on the screen
- Exit PHP
In this program, first the user declare variables as parameter in function. Then when the user call the function, passing the values to variables as argument. finally , the result is calculated and display on the screen
<head> <title> Subtraction of two numbers</title> </head> <body> <h3>PHP Subtract two numbers using function</h3> <?php //function definition function subTwoNumbers($x, $y) { $sub=$x - $y; return $sub; echo $sub; } //Calling the function and print result echo "The Difference of 10 and 300: "; echo subTwoNumbers(10,300); echo "<br>"; echo "The Difference of 200 and 120: "; echo subTwoNumbers(200,120); echo "<br>"; echo "The Difference of 100 and -200: "; echo subTwoNumbers(100,-200); echo "<br>"; ?> </body> </html>
When the above code is executed, it produces the following result
PHP Subtract two numbers using function
The Difference of 10 and 300: -290 The Difference of 200 and 120: 80 The Difference of 100 and -200: 300
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
Addition two number in C++ language
Addition two number in Python language