PHP intdiv() Function


Definition and Usage

The intdiv() function returns integer quotient of two integer parameters. If  x/y results in i as division and r as remainder so that

x = y*i+r 

In this case, intdiv(x,y) returns i 

Syntax

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

Parameters

Sr.NoParameter & Description
1x
This parameter forms numerator part of division expression
2y
This parameter forms denominator part of division expression

Return Values

PHP intdiv() function returns integer quotient of division of x by y. The return value is positive if both parameters are positive or both parameters are negative.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example shows that if numerator is < denominator, intdiv() function returns 0

<?php
   $x=10;
   $y=3;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "
";    $r=intdiv($y, $x);    echo "intdiv(" . $y . "," . $x . ") = " . $r; ?>

Output

This will produce following result −

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

Example

 Live Demo

In following example, intdiv() function returns negative integer because either numerator or denominator is negative −

<?php
   $x=10;
   $y=3;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "
";    $x=10;    $y=-3;    $r=intdiv($x, $y);    echo "intdiv(" . $x . "," . $y . ") = " . $r . "
";    $x=-10;    $y=3;    $r=intdiv($x, $y);    echo "intdiv(" . $x . "," . $y . ") = " . $r . "
";    $x=-10;    $y=-3;    $r=intdiv($x, $y);    echo "intdiv(" . $x . "," . $y . ") = " . $r ; ?>

Output

This will produce following result −

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

Example

 Live Demo

Denominator is 0 in following examplee. It results in DivisionByZeroError exception -1−

<?php
   $x=10;
   $y=0;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "
"; ?>

Output

This will produce following result −

PHP Fatal error: Uncaught DivisionByZeroError: Division by zero

Example

 Live Demo

Fractional part in both parametera is ignored and intdiv() function is applied only to integer parts −

<?php
   $x=2.90;
   $y=1.90;
   $r=intdiv($x, $y);
   echo "intdiv(" . $x . "," . $y . ") = " . $r . "
"; ?>

Output

This will produce following result −

intdiv(2.9,1.9) = 2

Updated on: 29-Jun-2020

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements