Home  >  Article  >  Backend Development  >  PHP intdiv() function

PHP intdiv() function

王林
王林forward
2023-09-18 19:05:111814browse

PHP intdiv() 函数

Definition and usage

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

Syntax

intdiv ( int $x , int $y ) : int

Parameters

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

Return value

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.

PHP Version

This function is available in PHP version 4.x, PHP 5.x and PHP 7.x.

Example

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;
?>

Output

This will produce the following result−

intdiv(10,3) = 3
intdiv(3,10) = 0

Example

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 ;
?>

Output

This will produce the following −

intdiv(10,3) = 3
intdiv(10,-3) = -3
intdiv(-10,3) = -3
intdiv(-10,-3) = 3

Example

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>";
?>

Output

This will produce the following result−

PHP Fatal error: Uncaught DivisionByZeroError: Division by zero

Example

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>";
?>

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete