PHP fmod() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The fmod() function returns the remainder after dividing one number by another, and it works with decimals.

It is like the % operator, but % only works with whole numbers while fmod() accepts floats.

Syntax

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

PHP
You pass a dividend and a divisor to fmod() and it returns the leftover part as a float.
CallWhat it does
fmod(10, 3)Returns 1, because 10 = 3 x 3 + 1.
fmod(10.5, 3)Returns 1.5.
fmod(9, 3)Returns 0, since 3 divides 9 evenly.

The result has the same sign as the first number, so fmod(-10, 3) is -1.

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

Parameters

ParameterDescription
num1Required. The dividend, the number that is divided.
num2Required. The divisor, the number you divide by.

Return Value

Return ValueDescription
floatThe floating point remainder of num1 / num2. It is NAN if num2 is 0.

PHP Examples

Example 1 — Find a remainder

Use fmod() with whole numbers and with decimals.

PHP
<?php
echo fmod(10, 3) . PHP_EOL;
echo fmod(10.5, 3) . PHP_EOL;
echo fmod(9, 3) . PHP_EOL;

How It Works

  1. 10 divided by 3 is 3 with 1 left over, so fmod(10, 3) is 1.
  2. 10.5 divided by 3 is 3 with 1.5 left over.
  3. 9 divided by 3 has nothing left, so the remainder is 0.
  4. The values are floats but PHP prints them in a short form.

Example 2 — Negative numbers and zero divisor

Check how the sign of the result is chosen and what happens with 0.

PHP
<?php
echo fmod(-10, 3) . PHP_EOL;
echo fmod(10, -3) . PHP_EOL;
echo fmod(5, 0) . PHP_EOL;

How It Works

  1. fmod(-10, 3) takes the sign of the first number, so the result is -1.
  2. fmod(10, -3) also follows the first number, so it is positive 1.
  3. fmod(5, 0) cannot be calculated, so PHP returns NAN.
  4. No exception is thrown, so check with is_nan() if the divisor can be zero.

Example 3 — Wrap an angle into 0-360 degrees

Keep any angle inside one full turn.

PHP
<?php
function normalizeAngle(float $angle): float {
    $result = fmod($angle, 360);

    if ($result < 0) {
        $result += 360;
    }

    return $result;
}

echo normalizeAngle(450) . PHP_EOL;
echo normalizeAngle(-90) . PHP_EOL;
echo normalizeAngle(720) . PHP_EOL;

How It Works

  1. fmod($angle, 360) removes all full turns from the angle.
  2. 450 becomes 90 because one full turn of 360 is removed.
  3. -90 stays negative after fmod(), so the function adds 360 to get 270.
  4. 720 is exactly two full turns, so the final angle is 0.

Frequently Asked Questions

It returns the floating point remainder of dividing the first number by the second.
The % operator converts values to integers first. fmod() keeps decimals, so fmod(10.5, 3) is 1.5.
It returns NAN. The % operator would throw DivisionByZeroError instead.
The result takes the sign of the first number, the dividend. So the answer is -1.
Yes. Add the divisor when the result is below zero, like the angle example above.

Next: fpow()

The fpow() function raises a number to a power and always returns a float.

fpow() 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