In PHP, you can use the arithmetic operator "/" to divide an integer by an integer. The division operator always returns a floating point number. Only when both operands are integers and can be divided exactly, then It returns an integer.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
How does php divide an integer by an integer?
$a / $b
Division The quotient of $a divided by $b.
$a % $b
Takes the remainder modulo $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. For integer division, please refer to intdiv().
The operands of the modulo operator will be converted to integers (except for the decimal part) before operation. For floating point number modulus, please refer to fmod().
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 ?>
Recommended: "PHP Video Tutorial"
The above is the detailed content of How to divide an integer by an integer in php. For more information, please follow other related articles on the PHP Chinese website!