Do you still remember the basic mathematics knowledge you learned in school? The arithmetic operators in php are just like them.
Example | Name | Result |
---|---|---|
-$ a | negates the negative value of | . |
$a + $b | Addition | The sum of the sum. |
$a - $b | Subtraction | The difference between the sum. |
$a * $b | The product of the multiplication | sum. |
$a / $b | Division | Quotient of division. |
$a % $b | takes the remainder modulo | . |
The division operator always returns a floating point number. The only exception is the following: both operands are integers (or stringsconverted to integers) 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 ?>
The above is the detailed content of Detailed explanation of arithmetic operators in php. For more information, please follow other related articles on the PHP Chinese website!