Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

intdiv()

Use intdiv for an integer quotient truncated toward zero.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

intdiv(int $num1, int $num2): int

Use intdiv for an integer quotient truncated toward zero. Opposite signs give a nonpositive quotient and small magnitudes can yield zero. A zero divisor raises DivisionByZeroError. Dividing PHP_INT_MIN by -1 raises ArithmeticError because its positive quotient is outside the integer range.

Signature

intdiv(int $num1, int $num2): int

Example output

[2,-2,0]
DivisionByZeroError
ArithmeticError

Examples

<?php
echo json_encode([intdiv(7, 3), intdiv(-7, 3), intdiv(2, -3)]), PHP_EOL;
foreach ([[1, 0], [PHP_INT_MIN, -1]] as [$x, $y]) {
    try { intdiv($x, $y); } catch (ArithmeticError $e) { echo get_class($e), PHP_EOL; }
}

Result

[2,-2,0]
DivisionByZeroError
ArithmeticError

Sources