Expressions: Arithmetic Operations
Addition
Addition can be used to add two values using the +
operator, followed by the value to add to the left.
$foo = $bar + $baz;
Subtraction
Subtraction can be used to subtract two values using the -
operator, followed by the value to subtract from the left.
$foo = $bar - $baz;
Multiplication
Multiplication can be used to multiply two values using the *
operator, followed by the value to multiply to the left.
$foo = $bar * $baz;
Division
Division can be used to divide two values using the /
operator, followed by the value to divide from the left.
$foo = $bar / $baz;
Modulus
Modulus can be used to get the remainder of two values using the %
operator, followed by the value to divide from the left.
$foo = $bar % $baz;
Exponentiation
Exponentiation can be used to exponentiate two values using the **
operator, followed by the value to exponentiate to the left.
$foo = $bar ** $baz;
Unary Plus
Unary plus can be used to convert a value to a positive number using the +
operator.
$foo = +$bar;
Unary Minus
Unary minus can be used to convert a value to a negative number using the -
operator.
$foo = -$bar;
Pre Increment
Pre increment can be used to increment a value by one using the ++
operator.
$foo = ++$bar;
Post Increment
Post increment can be used to increment a value by one using the ++
operator.
$foo = $bar++;
INFO
The difference between pre and post increment is that pre increment will increment the value before the expression is evaluated, while post increment will increment the value after the expression is evaluated.
Pre Decrement
Pre decrement can be used to decrement a value by one using the --
operator.
$foo = --$bar;
Post Decrement
Post decrement can be used to decrement a value by one using the --
operator.
$foo = $bar--;
INFO
The difference between pre and post decrement is that pre decrement will decrement the value before the expression is evaluated, while post decrement will decrement the value after the expression is evaluated.