PHP Numeric values operators

Register here to save your progress

? ? ?

Arithmetic Operators

Arithmetic operators are used to perform normal arithmetical operations among variables or variables and other mathematical values. With them you can perform addition, multiplication, subtraction etc.

Name Operator Example Result
Addition + $a + $b Sum of the variables $a and $b
Subtraction - $a - $b Difference of the variables $a and $b
Multiplication * $a * $b Product of the variables $a and $b
Division / $a / $b Partition of variables $a and $b
Modulus % $a % $b Remainder of the variable $a divided by $b
Exponentiation ** $a ** $b Result of raising the variable $a to the $b'th power

Practical example: 

<?php

$a = 10;
$b = 4;

//Addition
$addition = $a + $b;
echo $addition."\n";
// outputs 14

$subtraction = $a - $b;
echo $subtraction."\n";
//outputs 6

$multiplication = $a * $b;
echo $multiplication."\n";
//outputs 40

$division = $a / $b;
echo $division."\n";
//outputs 2.5

$modulus = $a % $b;
echo $modulus."\n";
/*outputs 2 
  8 is the maximum multiplication number that 4 can have 
  inside 10, so we are left with remaining 2 */

$exponentiation = $a ** $b;
echo $exponentiation."\n";
//outputs 10000

What is the output othe following code:

<?php

$a = 15;

echo ($a % 13)**2;

?>

Assignment Operators

The assignment operator are used to write (assign) a numberic value to a variable. The default assignment operator in PHP is "=" and it assigns to the variable from the left side the value of the expression from the right side.

Assignment Equivalent Result
$a = $b $a = $b The variable from left will get the value of the expression from right
$a += $b $a = $a + $b Addition
$a -= $b $a = $a - $b Subtraction
$a *= $b $a = $a * $b Multiplication
$a /= $b $a = $a / $b Division
$a %= $b $a = $a % $b Modulus

What is the output of the following code?

<?php

$a = 33;
$b = 3;
$c = 7;

$a /= $b;
$a %= $c;

echo $a;

?>

Increment / Decrement Operators

The incremental operator increments the variables value by 1. The decremental operator decrements the variable's value by 1.

Operator Name Result
++$a Pre-increment Increments $a by 1, then returns the value of $a
$a++ Post-increment Returns the value of $a, then increments $a by 1
--$a Pre-decrement Decrements $a by 1, then returns the value of $a
$a-- Post-decrement Returns the value of $a, then decrements $a by 1

Practical example:

<?php

$a = 5;

++$a;
//the value of $a is now 6

$a++;
//the value of $a is now 7

--$a;
//the value of $a is now 6

$a--;
//the value of $a is now 5

$a = 5;
$b = 0;
$b = ++$a;
/* the value of $b is 6 and also the value of $a is 6, 
   since $a has first been incremented by 1 
   and then it's value has been returned to $b
*/

$a = 5;
$b = 0;
$b = $a++;
/* the value of $b is 5 and the value of $a is 6, 
   since $a has first returned the initial value of 5
   and then it's value has been incremented by 1 
*/

$a = 5;
$b = 0;
$b = --$a;
/* the value of $b is 4 and also the value of $a is 4, 
   since $a has first been decremented by 1 
   and then it's value has been returned 
*/

$a = 5;
$b = 0;
$b = $a--;
/* the value of $b is 5 and the value of $a is 4, 
   since $a has first returned the initial value of 5
   and then it's value has been decremented by 1 
*/

?>

Are the variables $a and $b equal at the end of the script?

<?php

$a = 10;
$b = 10;
$c = 2;

$a += $c++;

$b++;
++$b;

//end of the script

?>