Calculations

Function to subtract two numbers in PHP

Function to subtract two numbers in PHP

In this tutorial, we will discuss the concept of Function to subtract two numbers in PHP

In this topic, we are going to learn how  to write a program to subtract two numbers using user-defined function in  PHP programming language

Find Difference of two numbers

Function to subtract two numbers

Find difference of two numbers using function – #1

In this program, the user declare two integer variables $x and $y as parameters of the user defined function. Then two numbers are subtracted first value from second value using minus(-) operator. finally when the user call the function with argument, the result is displayed on the screen

Program 1

<?php //php script
//function definition
function subTwoNumbers($x, $y)
{
    return $x - $y;//calculate the difference using minus
}
//Calling the function and print result
echo "The difference of 200 and 30: ".subTwoNumbers(200,30);
echo "<br>";
echo "The difference of 20 and 30: ".subTwoNumbers(20,30);
echo "<br>";
echo "The difference of 10 and 110: ".subTwoNumbers(10,110);
echo "<br>";
echo "The difference of -100 and -80: ".subTwoNumbers(-100,-80);
echo "<br>";
//exit php
?>

When the above code is executed, it produces the following result

The difference of 200 and 30: 170
The difference of 20 and 30: -10
The difference of 10 and 110: -100
The difference of -100 and -80: -20

 

 

Find difference of two numbers using function – #2

In this program, the user declare two integer variables $x and $y as parameters of the function. Then two numbers are subtracted first value from second value using minus(-) operator. finally when the user call the function with argument, the result is displayed on the screen

Program 2

<?php //php script
//function definition
function subTwoNumbers($x, $y)
{
    $sub=$x - $y;
    //calculate difference using minus operator
    return $sub;//return the result
    echo $sub;//print the result when calling the function
}

//Calling the function and print result
echo "The Difference of 20 and 300: ";
echo subTwoNumbers(20,300);
echo "<br>";
echo "The Difference of 200 and 120: ";
echo subTwoNumbers(200,120);
echo "<br>";
echo "The Difference of 10 and -190: ";
echo subTwoNumbers(10,-190);
echo "<br>";
//exit php
?>

When the above code is executed, it produces the following result

The Difference of 20 and 300: -280
The Difference of 200 and 120: 80
The Difference of 10 and -190: 200

 

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

 

PHP Addition of two numbers

PHP Addition of two numbers using function

 

 

PHP subtracting two numbers-PHP program
PHP program to multiply two numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago