PHP fdiv() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The fdiv() function divides one number by another and always returns a float, even when you divide by zero.

Instead of throwing an error like intdiv() or the % operator, fdiv() follows the IEEE 754 rules and returns INF, -INF or NAN.

Syntax

Call fdiv() with the values you need. Start with this line.

PHP
You pass two numbers to fdiv() and it returns the first divided by the second as a float. It is available since PHP 8.0.
CallWhat it does
fdiv(10, 4)Returns 2.5.
fdiv(9, 3)Returns 3 as a float.
fdiv(1, 0)Returns INF instead of throwing an error.

In PHP 8 the / operator throws DivisionByZeroError when the divisor is 0. Use fdiv() when you want a value back instead.

Library declarations
PHP
fdiv(float $num1, float $num2): float

Parameters

ParameterDescription
num1Required. The dividend, the number that will be divided.
num2Required. The divisor, the number you divide by. It can be 0.

Return Value

Return ValueDescription
floatThe result of num1 / num2. It can be INF, -INF or NAN when the divisor is 0.

PHP Examples

Example 1 — Normal division

When the divisor is not zero, fdiv() works like the / operator.

PHP
<?php
echo fdiv(10, 4) . PHP_EOL;
echo fdiv(9, 3) . PHP_EOL;
echo round(fdiv(1, 3), 4) . PHP_EOL;

How It Works

  1. fdiv(10, 4) divides 10 by 4 and returns the float 2.5.
  2. fdiv(9, 3) gives exactly 3, and PHP prints the float 3.0 as 3.
  3. fdiv(1, 3) gives a long decimal, so round() limits it to four places.
  4. The return type is always float, even when the answer looks like a whole number.

Example 2 — Dividing by zero

See the three special values you can get when the divisor is 0.

PHP
<?php
echo fdiv(1, 0) . PHP_EOL;
echo fdiv(-1, 0) . PHP_EOL;
echo fdiv(0, 0) . PHP_EOL;

How It Works

  1. A positive number divided by 0 gives INF (infinity).
  2. A negative number divided by 0 gives -INF (negative infinity).
  3. 0 divided by 0 has no meaningful answer, so the result is NAN (not a number).
  4. None of these cases throw an exception, unlike the / operator.

Example 3 — Safe average calculation

Write a small function that does not crash when there is no data.

PHP
<?php
function average(float $total, int $count): string {
    $result = fdiv($total, $count);

    if (is_infinite($result) || is_nan($result)) {
        return "No data";
    }

    return (string) $result;
}

echo average(50, 4) . PHP_EOL;
echo average(50, 0) . PHP_EOL;

How It Works

  1. The function divides the total by the count using fdiv().
  2. When count is 4 the result is 12.5, which is a normal finite number.
  3. When count is 0 the result is INF, so is_infinite() catches it.
  4. The function then returns a friendly message instead of crashing.

Frequently Asked Questions

It is a function added in PHP 8.0 that divides two numbers and returns a float, following IEEE 754 rules for division by zero.
With / a zero divisor throws DivisionByZeroError. fdiv() returns INF, -INF or NAN instead.
It returns NAN, which means not a number.
No. fdiv() was added in PHP 8.0, so older versions will give an undefined function error.
Use is_infinite() to detect INF or -INF, and is_nan() to detect NAN. is_finite() is true only for normal numbers.

Next: floor()

The floor() function rounds a number down to the nearest whole number.

floor() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful