PHP intdiv() function

王林
Release: 2023-09-18 19:06:02
forward
1823 people have browsed it

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
Copy after login

Parameters

Sr.NoParameters and description
1 x< /p>

This parameter forms the numerator part of the division expression

2y

This parameter forms the denominator part of the division expression

Return value< /h2>

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;
?>
Copy after login

Output

This will produce the following result−

intdiv(10,3) = 3
intdiv(3,10) = 0
Copy after login

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 ;
?>
Copy after login

Output

This will produce the following −

intdiv(10,3) = 3
intdiv(10,-3) = -3
intdiv(-10,3) = -3
intdiv(-10,-3) = 3
Copy after login

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>";
?>
Copy after login

Output

This will produce the following result−

PHP Fatal error: Uncaught DivisionByZeroError: Division by zero
Copy after login

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>";
?>
Copy after login

Output

This will produce the following result-

intdiv(2.9,1.9) = 2
Copy after login

The above is the detailed content of PHP intdiv() function. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template