Google Plus Facebook Twitter RSS YouTube

Subtraction of Numbers using PHP Programming

Subtraction is a mathematical arithmetic operation. It performs subtraction of two or more numbers. It is not only in mathematics but also often has to perform in PHP programming to get the resultant value of subtraction of two or more numbers. Mathematically, subtraction of two or more numbers is calculated by placing an operator (called minus (-)) between them. The same fundamental method is also applied in php programming to make PHP to calculate the subtraction of numerals.

Subtraction in PHP Programming

Program

First of all consider two variable p and q but in PHP programming each variable is defined with $ symbol. Therefore, assumed variables are denoted $p and $q in PHP Hypertext Preprocessor script. Place them inside the PHP script by placing an operator minus (-) between them to allow them to perform arithmetic operation mathematically.

  1. First step is, define syntax of the PHP program by writing start and end tags of PHP syntax.
  2. Write variables inside the syntax and place a minus symbol between them.
  3. The value of subtraction is also a variable. Assume its result is $r.
  4. Print resultant value of subtraction of two or more variables by outputting with one of outputting functions. In this example print() function is used.

PHP Code

<?php

  $r = $p - $q;
  print $r;

?>

As per above principle PHP program, programmers can perform subtraction of two or more numbers easily.

Example

This arithmetical operation can be studied practically by giving values to assumed variables. In this example, assume the value of $p is 3 and the value of another variable $q is 91. The subtraction of both variables is a variable which is known $r. When we input values to the variables, the mathematical equation in PHP program performs subtractions and stores resultant value in $r. Output resultant value in web browser by outputting functions like print.

PHP Code

<?php

  $p = 3;
  $q = 91;
  
  $r = $p - $q;
  
  print "Subtraction = ".$r;

?>

Placing the above code in a PHP web document, executes the program. The output function gets the resultant value from variable $r and prints the result as it is in web browser as exampled below.

Output in Web Browser

Subtraction = -88

In this way, php programmers perform the arithmetical operations like subtraction by using a php program. The same procedure can also apply when we deal more than two variables.

Follow Us
Google Plus Facebook Twitter RSS YouTube
Subscribe via Email
Save or Share This PHP Tutorial with Your Friends
Save or Share PHP Tutorial