PHP Arithmetic Operators
Description
The arithmetic operators handle basic numerical operations, such as addition and multiplication.
PHP Arithmetic Operators
The full list arithmetic operators in PHP is shown in the following table.
Operator | Meaning | Operation |
---|---|---|
+ | Addition | Returns the first value added to the second: $a + $b. |
- | Subtraction | Returned the second value subtracted from the first: $a - $b. |
* | Multiplication | Returns the first value multiplied by the second: $a * $b. |
/ | Division | Returns the first value divided by the second: $a / $b. |
% | Modulus | Divides the first value into the second, then returns the remainder: $a % $b. This only works on integers, and the result will be negative if $a is negative. |
+= | Shorthand addition | Adds the second value to the first: $a += $b. Equivalent to $a = $a + $b. |
-= | Shorthand subtraction | Subtracts the second value from the first: $a -= $b. Equivalent to $a = $a - $b. |
*= | Shorthand multiplication | Multiplies the first value by the second: $a *= $b. Equivalent to $a = $a * $b. |
/= | Shorthand division | Divides the first value into the second: $a /= $b. Equivalent to $a = $a / $b. |
An exponentiation is done via the pow() function. Here are some examples.
<?PHP/* www. j a v a2 s. co m*/
$a = 13;
$b = 4;
$c = 3.33;
$d = 3.99999999;
$e = -10;
$f = -4;
print $a + $b;
print "\n";
print $a - $c;
print "\n";
print $a * $d;
print "\n";
print $a / $f;
print "\n";
print $e % $b;
?>
The code above generates the following result.