Mathematical Operations In PHP

With PHP we can easily manage mathematical operations in a very intuitive way. In this lesson of our guide, we will learn how to count within our applications.

Mathematical Operators Of PHP

The main mathematical operators foreseen by the PHP syntax are:

OperatorOperation
+Addition
Subtraction
*Multiplication
/Division
%Module

The only mathematical operator to request some clarification is probably the Modulo operator which is used to calculate the remainder of a division. Eg:

10 % 4 = 2

(4 is in 10 only once with the remainder of 2).

Simple Math Operations With PHP

Let’s do some examples of calculations with PHP also using variables.

// define variable
$a = 8;
$b = 2;

// Summation
$sum = ($a + $b);
echo $a . " + " . $b . " = " . $sum  . "<br>";

// Substraction
$sub = ($a - $b);
echo $a . " - " . $b . " = " . $sub . "<br>";

// Multiplication
$mul = ($a * $b);
echo $a . " x " . $b . " = " . $mul . "<br>";

// Division
$div = ($a / $b);
echo $a . " : " . $b . " = " . $div . "<br>";

In the example that follows we will make a simple calculation of the expenditure: let’s buy 4 apples at 50 cents each … how much did we spend? Let’s see it with a simple PHP script:

<?php
$apple = 4;
$dollar = 0.5;
$total = $apple * $dollar;

echo "Total:  " . $total. " Dollar";
?>

Note : in the example we used two numeric variables: one ($ apples) of type integer (integer) and the duck ($ euro) of type double (decimal number).

“Complex” Mathematical Operations

Remember that in managing mathematical operations with PHP it is important … to respect the mathematical rules! Let’s take an example:

$total = 5+7*2;

it’s different from:

$total = (5+7)*2;

It is therefore very important to respect the “priorities” of the mathematical calculation by using the correct parentheses because, otherwise, we could obtain unexpected results. Let’s see an example of a complex operation that requires the use of parentheses:

<?php
//define variable
$a = 8;
$b = 2;
$c = 5;
$d = 4;

$total = ((($a + $b) / $c) * $d);
echo "Total is: " . $total;
?>

Increase And Decrease A Numeric Variable

PHP allows you to easily increase and decrease the value of a numeric variable. To carry out the increase, use the + = operator followed by the number you want to add, in this way:

$number = 10;

//add 5 with number
$number += 5;

echo $number; //Output 15

After this operation, our variable $ number will no longer have the original value (10), but the new one given by the increase of 5 (therefore 15).

Mirror speech for the operator – = which will be used to decrease a numeric variable of the following value:

$number = 10;

//add 5 with number
$number -= 5;

echo $number; //Output 5

Furthermore, PHP provides shortcuts to increase or decrease a given numeric variable by one unit; but let’s go back to the usual example of before … To increase the value of the variable $ number by one (+1) we can simply write the no, and the variable followed by two plus symbols (++) in this way:

$number = 10;

//add 5 with number
$number ++;

echo $number; //Output 11

To decrease the value of one (-1) we can write the name of the variable followed by two minuses (-) in this way:

$number = 10;

//add 5 with number
$number ++;

echo $number; //Output 9

In other words:

  • $ Var ++ corresponds to $ var + 1;
  • $ var– corresponds to $ var-1;

Leave a Comment