Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
php

What is the intdiv() function in PHP?

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The intdiv() function in PHP divides two integers and returns a quotient.

Syntax

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

Parameters

The function requires two parameters:

  • num1, which should be an integer.
  • num2, which should be another integer (it shouldn’t be zero).

Return value

The function returns the quotient of num1/num2.

Note: The function will throw an exception in two cases:

  • If num2 is 0, the function throws a DivisionByZeroError exception.
  • If num1 is PHP_INT_MIN and num2 is -1, the function throws an ArithmeticError exception.

Code

In this example, we will see how to use the intdiv() function and how to handle exceptions:

<?php
echo intdiv(5, 2) . "\n";
try {
echo intdiv(1, 0);
} catch(DivisionByZeroError $e){
echo "Cant divide by 0\n";
}
try {
echo intdiv(PHP_INT_MIN, -1);
} catch(ArithmeticError $e){
echo "Cant divide " . ((string) PHP_INT_MIN). " by -1";
}
?>
Example of the intdiv() function in PHP

RELATED TAGS

communitycreator
php

CONTRIBUTOR

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring