This is a beginners tutorial on how to do basic subtraction in PHP.
The aim of this tutorial is to help developers who are completely new to the language.
Simple subtraction.
To subtract numbers in PHP, we can use the Subtraction Arithmetic Operator. Take a look at the following example.
//Subtract 2 from 20. echo 20 - 2;
In the PHP code above, we subtracted 2 from 20. If you run the above code, the number 18 will be printed out onto the page. This is because 20 minus 2 is 18.
Assigning the result to a variable.
If you want to assign the result of your subtraction arithmetic to a PHP variable, you can do something like this.
//Subtract 10 from 30 and assign the //result to a variable called $answer. $answer = 30 - 10; //You can then print out the variable $answer echo $answer;
In the example above, we subtracted 10 from 30 and assigned the result to a PHP variable called $answer.
If you print the $answer variable out onto the page, you will see the number 20. That is because 30 minus 10 is 20.
It is important to note that in this case, our $answer variable will be an integer. This is because the numbers involved in our subtraction arithmetic were both integers.
Subtracting variables.
You can also do something like this.
//Create the larger number. $a = 40; //Create the smaller number. $b = 25; //Do the subtraction and assign the result to a variable //called $answer. $answer = $a - $b; //Print the answer out onto the page. echo $answer;
In the code above, we created two variables containing integer numbers and then subtracted them.
Negative numbers.
If you subtract a larger number from a smaller number, you will end up with a minus figure.
//Our first number. $a = 40; //Our second number. $b = 50; //Do the subtraction. echo $a - $b;
If you run the code above, you will see that the number -10 is printed out onto the page. This is because if you subtract 50 from 40, the result is minus 10.
Decimal numbers.
Up until this point, we have only been dealing with integers. However, what happens if we use a decimal number?
<?php //Our first number. $a = 10.5; //Our second number. $b = 5; //Do the subtraction. $result = $a - $b; //Print the result. echo $result;
The code above will print out the number 5.5. This is because 10.5 minus 5 is equal to 5.5.
Note that in this case, the $result variable will be a float value. This is because one of the values used in our arithmetic was a float variable (10.5).
Subtract from the result.
If you store the result of your subtraction in a PHP variable, you can subtract even further.
//Our first number. $a = 10; //Our second number. $b = 5; //Do the subtraction and assign it to a variable called $result. $result = $a - $b; //Print out the $result variable. echo $result . '<br>'; //Subtract 3 from the $result. $result = $result - 3; //Print out the $result variable again. echo $result . '<br>';
The PHP code above will print out the numbers 5 and 2. This is because 10 minus 5 gives us 5. Then, we subtracted 3 from 5, which gave us 2.