Divide Two Numbers using fdiv in PHP 8.0

Divide Two Numbers using fdiv in PHP 8.0

Since PHP 7.0, we can use the intdiv function to perform the integer division. It is a mathematical operation that divides two numbers and returns the integer part by removing the fractional part (remainder).

<?php

echo intdiv(7, 2); // 3

The intdiv function throws a DivisionByZeroError when dividing a number by zero.

<?php

echo intdiv(7, 0);

Example will output:

Fatal error: Uncaught DivisionByZeroError: Division by zero in main.php:3

Since PHP 8.0, we can use fdiv function to perform the floating-point division based on IEEE 754 standard. This mathematical operation divides two numbers and returns the floating-point number.

<?php

echo fdiv(7, 2); // 3.5

When a number is divided by zero, the fdiv function returns +INF, -INF or NAN.

<?php

echo fdiv(7, 0);  // INF
echo fdiv(-7, 0); // -INF
echo fdiv(0, 0);  // NAN

Leave a Comment

Cancel reply

Your email address will not be published.