Home > Article > Backend Development > PHP intdiv() function
intdiv()The function returns the integer quotient of two integer parameters. If the result of x/y is i as division, r as remainder, such that
x = y*i r
In this case, intdiv(x,y) returns i
intdiv ( int $x , int $y ) : int
Sr.No | Parameters and description |
---|---|
1 | x This parameter forms the numerator part of the division expression |
2 |
y This parameter forms the denominator part of the division expression |
PHP intdiv( ) function returns the integer quotient of x divided by y. If both arguments are positive or both arguments are negative, the return value is positive.
This function is available in PHP version 4.x, PHP 5.x and PHP 7.x.
Real-time demonstration
The following example shows that if the numerator is < the denominator, the intdiv() function returns 0
<?php $x=10; $y=3; $r=intdiv($x, $y); echo "intdiv(" . $x . "," . $y . ") = " . $r . "</p><p>"; $r=intdiv($y, $x); echo "intdiv(" . $y . "," . $x . ") = " . $r; ?>
This will produce the following result−
intdiv(10,3) = 3 intdiv(3,10) = 0
Live Demonstration
In the following example, the intdiv() function returns a negative integer because the numerator or denominator is negative -
<?php $x=10; $y=3; $r=intdiv($x, $y); echo "intdiv(" . $x . "," . $y . ") = " . $r . "</p><p>"; $x=10; $y=-3; $r=intdiv($x, $y); echo "intdiv(" . $x . "," . $y . ") = " . $r . "</p><p>"; $x=-10; $y=3; $r=intdiv($x, $y); echo "intdiv(" . $x . "," . $y . ") = " . $r . "</p><p>"; $x=-10; $y=-3; $r=intdiv($x, $y); echo "intdiv(" . $x . "," . $y . ") = " . $r ; ?>
This will produce the following −
intdiv(10,3) = 3 intdiv(10,-3) = -3 intdiv(-10,3) = -3 intdiv(-10,-3) = 3
Live Demonstration
The denominator in the following example is 0 . It will cause DivisionByZeroError exception -1−
<?php $x=10; $y=0; $r=intdiv($x, $y); echo "intdiv(" . $x . "," . $y . ") = " . $r . "</p><p>"; ?>
This will produce the following result−
PHP Fatal error: Uncaught DivisionByZeroError: Division by zero
Live Demonstration
The decimal part in both parameters is ignored and the intdiv() function is applied only to the integer part -
<?php $x=2.90; $y=1.90; $r=intdiv($x, $y); echo "intdiv(" . $x . "," . $y . ") = " . $r . "</p><p>"; ?>
This will produce the following result-
intdiv(2.9,1.9) = 2
The above is the detailed content of PHP intdiv() function. For more information, please follow other related articles on the PHP Chinese website!