Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / PHP

intdiv()

La función intdiv divide enteros y trunca el cociente hacia cero.

Versión: PHP 8.4; repository runtime, production runtime not attested

Sintaxis

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

La función intdiv divide enteros y trunca el cociente hacia cero. Signos contrarios dan un valor no positivo, a veces cero. Un divisor cero provoca DivisionByZeroError. Para PHP_INT_MIN y -1 el cociente positivo excede int y provoca ArithmeticError.

Firma

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

Salida del ejemplo

[2,-2,0]
DivisionByZeroError
ArithmeticError

Ejemplos

<?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; }
}

Resultado

[2,-2,0]
DivisionByZeroError
ArithmeticError

Fuentes