Home > Article > Backend Development > php arithmetic operators
Remember the basic math knowledge you learned in school? Just like them.
Arithmetic Operator
Example Name Result
-$a Negative value of $a.
$a + $b Addition The sum of $a and $b.
$a - $b Subtraction The difference between $a and $b.
$a * $b Multiplication The product of $a and $b.
$a / $b Division quotient of $a divided by $b.
$a % $b Modulo The remainder of $a divided by $b.
The division operator always returns a floating point number. The only exception is that both operands are integers (or integers converted from strings) and are exactly divisible, in which case it returns an integer.
The operands of the modulo operator will be converted to integers (except for the decimal part) before operation.
The result of the modulo operator % is the same as the sign (positive or negative sign) of the dividend. That is, the result of $a % $b has the same sign as $a. For example:
<?php echo (5 % 3)."\n"; // prints 2 echo (5 % -3)."\n"; // prints 2 echo (-5 % 3)."\n"; // prints -2 echo (-5 % -3)."\n"; // prints -2 ?>