PHP Function Reference

PHP fmod() Function



The PHP fmod() function returns remainder (modulo) of the division of specified arguments. This can be mathematically expressed as below:

remainder = x - quot * y

Where quot is the result of x/y rounded toward zero. If y is non-zero, then remainder has the same sign as x and a magnitude less than the magnitude of y.

In special cases it returns the following:

  • If any of the argument is NAN or both of the arguments are infinity, then the result is NAN.
  • If the first argument is infinity and second argument is finite, then the result is NAN.
  • If the second argument is zero, then the result is NAN.
  • If the first argument is finite and second argument is infinity, then the result is same as first argument.

Syntax

fmod(x, y)

Parameters

x Required. Specify the dividend.
y Required. Specify the divisor.

Return Value

Returns remainder of the division of arguments (x/y).

Example:

In the example below, fmod() function is used to return the remainder of division of specified arguments.

<?php
echo "fmod(25, 7) = ".fmod(25, 7)."\n";
echo "fmod(-25, 7) = ".fmod(-25, 7)."\n";
echo "fmod(25, -7) = ".fmod(25, -7)."\n";
echo "fmod(-25, -7) = ".fmod(-25, -7)."\n";
echo "fmod(25, NAN) = ".fmod(25, NAN)."\n";
echo "fmod(NAN, 7) = ".fmod(NAN, 7)."\n";
echo "fmod(25, INF) = ".fmod(25, INF)."\n";
echo "fmod(INF, 7) = ".fmod(INF, 7)."\n";
?>

The output of the above code will be:

fmod(25, 7) = 4
fmod(-25, 7) = -4
fmod(25, -7) = 4
fmod(-25, -7) = -4
fmod(25, NAN) = NAN
fmod(NAN, 7) = NAN
fmod(25, INF) = 25
fmod(INF, 7) = NAN

❮ PHP Math Reference