Php 简明教程
PHP – Integer Division
PHP 引入了一个新函数 intdiv(),它执行其操作数的整数除法,并返回除法结果作为 int。
PHP has introduced a new function intdiv(), which performs integer division of its operands and return the division as int.
intdiv() 函数返回两个整数参数的整数商。如果 “a/b” 除法的结果是 “c”,余数是 “r” −
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
在这种情况下, intdiv(a,b) 返回 r −
In this case, intdiv(a,b) returns r −
intdiv ( int $x , int $y ) : int
$x 和 $y 是除法表达式的分子和分母部分。intdiv() 函数返回一个整数。如果两个参数都是正数或负数,则返回值为正数。
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
如果分子 < 分母,则 intdiv() 函数返回 “0”,如下所示 −
If numerator is < denominator, the intdiv() function returns "0", as shown below −
<?php
$x=10;
$y=3;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
$r=intdiv($y, $x);
echo "intdiv(" . $y . "," . $x . ") = " . $r;
?>
它将生成以下 output −
It will produce the following output −
intdiv(10,3) = 3
intdiv(3,10) = 0
Example 2
在以下示例中,intdiv() 函数返回负整数,因为分子或分母是负数。
In the following example, the intdiv() function returns negative integer because either the numerator or denominator is negative.
<?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";
?>
它将生成以下 output −
It will produce the following output −
intdiv(10,-3) = -3
intdiv(-10,3) = -3
Example 3
分子和分母都是正数或都是负数的情况下,intdiv() 函数返回正整数。
The intdiv() function returns a positive integer in the case of numerator and denominator both being positive or both being negative.
<?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 ;
?>
它将生成以下 output −
It will produce the following output −
intdiv(10,3) = 3
intdiv(-10,-3) = 3
Example 4
在以下示例中,分母为 “0”。它导致 DivisionByZeroError 异常。
In the following example, the denominator is "0". It results in DivisionByZeroError exception.
<?php
$x=10;
$y=0;
$r=intdiv($x, $y);
echo "intdiv(" . $x . "," . $y . ") = " . $r . "\n";
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught DivisionByZeroError: Division by zero in hello.php:4