PHP has introduced a new function intdiv(), which performs integer division of its operands and return the division as int.
The intdiv() function returns integer quotient of two integer parameters. If “a/b” results in “c” as division and “r” as remainder such that −
a=b*c+r
In this case, intdiv(a,b) returns r −
intdiv(int$x,int$y):int
The $x and $y are the numerator and denominator parts of the division expression. The intdiv() function returns an integer. The return value is positive if both parameters are positive or both parameters are negative.
Example 1
If numerator is < denominator, the intdiv() function returns “0”, as shown below −
Open Compiler
<?php
$x=10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$r=intdiv($y, $x);
echo "intdiv(" . $y . "," . $x . ") = " . $r;
?>
It will produce the following output −
intdiv(10,3) = 3
intdiv(3,10) = 0
https://googleads.g.doubleclick.net/pagead/ads?gdpr=0&client=ca-pub-7133395778201029&output=html&h=280&twa=1&adk=2119661962&adf=985377881&w=553&abgtt=5&fwrn=4&fwrnh=100&lmt=1728310840&rafmt=1&armr=4&channel=6814194163&format=553×280&url=https%3A%2F%2Fwww.tutorialspoint.com%2Fphp%2Fphp_integer_division.htm&fwr=0&fwrattr=true&rs=1&rh=90&rw=553&rpe=1&resp_fmts=3&wgl=1&uach=WyJXaW5kb3dzIiwiMTUuMC4wIiwieDg2IiwiIiwiMTI5LjAuNjY2OC45MCIsbnVsbCwwLG51bGwsIjY0IixbWyJHb29nbGUgQ2hyb21lIiwiMTI5LjAuNjY2OC45MCJdLFsiTm90PUE_QnJhbmQiLCI4LjAuMC4wIl0sWyJDaHJvbWl1bSIsIjEyOS4wLjY2NjguOTAiXV0sMF0.&dt=1728769343022&bpp=4&bdt=17607&idt=4&shv=r20241009&mjsv=m202410080101&ptt=9&saldr=aa&abxe=1&cookie=ID%3Def5125dd405cf358%3AT%3D1727695895%3ART%3D1728769142%3AS%3DALNI_MahPzT8nN1o63zRofvuk-Rsfey3nQ&gpic=UID%3D00000f15567e3070%3AT%3D1727695895%3ART%3D1728769142%3AS%3DALNI_Ma-PCM2ZJFpdRT5-b5bqKepXAFJbg&eo_id_str=ID%3D911157ab19947e6f%3AT%3D1727695895%3ART%3D1728769142%3AS%3DAA-AfjZ2C3suFSN8H6IXiG2ZGVXD&prev_fmts=320×100%2C553x280&correlator=3402462170275&pv_ch=6814194163%2B&frm=20&pv=1&u_tz=300&u_his=34&u_h=864&u_w=1536&u_ah=864&u_aw=1536&u_cd=24&u_sd=1.25&dmc=8&adx=437&ady=1416&biw=903&bih=743&scr_x=0&scr_y=1211&eid=44759876%2C44759927%2C44759837%2C42531705%2C95332589%2C95338242%2C95343454%2C95344777&oid=2&pvsid=2236781938738545&tmod=982169104&uas=3&nvt=1&ref=https%3A%2F%2Fwww.tutorialspoint.com%2Fphp%2Fphp_use_statement.htm&fc=896&brdim=-7%2C0%2C-7%2C0%2C1536%2C0%2C935%2C871%2C920%2C743&vis=1&rsz=M%7C%7CoeE%7C&abl=CS&pfx=0&fu=128&bc=31&bz=1.02&td=1&tdf=2&psd=W251bGwsbnVsbCxudWxsLDNd&nt=1&ifi=14&uci=a!e&fsb=1&dtd=22
Example 2
In the following example, the intdiv() function returns negative integer because either the numerator or denominator is negative.
Open Compiler
<?php
$x=10;
$y=-3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$x=-10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
?>
It will produce the following output −
intdiv(10,-3) = -3
intdiv(-10,3) = -3
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
Example 3
The intdiv() function returns a positive integer in the case of numerator and denominator both being positive or both being negative.
Open Compiler
<?php
$x=10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$x=-10;
$y=-3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r ;
?>
It will produce the following output −
intdiv(10,3) = 3
intdiv(-10,-3) = 3
Example 4
In the following example, the denominator is “0”. It results in DivisionByZeroError exception.
Open Compiler
<?php
$x=10;
$y=0;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
?>
It will produce the following output −
PHP Fatal error: Uncaught DivisionByZeroError: Division by zero in hello.php:4
Leave a Reply