PHP fmod() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The name fmod stands for floating modulo . This function returns remainder of division of two floating point numbers. If  x/y results in i as division and r as remainder so that

x = y*i+r 

In this case, fmod(x,y) returns r

Syntax

fmod ( float $x , float $y ) : float

Parameters

Sr.NoParameter & Description
1x
This parameter forms numerator part of division expression
2y
This parameter forms denominator part of division expression

Return Values

PHP fmod() function returns floating point remainder of the act of division of x by y. Remainder carries sign of x.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

Following example calculates remaainder of division of 5.25 with 1.5 and returns 0.75

<?php
   $x=5.25;
   $y=1.5;
   $r=fmod($x, $y);
   echo "fmod(" . $x . "," . $y . ") = " . $r;
?>

Output

This will produce following result −

fmod(5.25,1.5) = 0.75

Example

 Live Demo

Following example has a negative numerator. Hence, remainder is also negative.−

<?php
   $x=-10;
   $y=3;
   $r=fmod($x, $y);
   echo "fmod(" . $x . "," . $y . ") = " . $r;
?>

Output

This will produce following result −

fmod(-10,3) = -1

Example

 Live Demo

Both parameters (-10 and -3) in following example are negative. As the remainder carries sign of numerator, result is -1−

<?php
   $x=-10;
   $y=-3;
   $r=fmod($x, $y);
   echo "fmod(" . $x . "," . $y . ") = " . $r;
?>

Output

This will produce following result −

fmod(-10,-3) = -1

Example

 Live Demo

If numerator is 0, denominator is non-zero, remainder is 0. If denominator is 0, remainder is NAN −

<?php
   $x=0;
   $y=3.33;
   $r=fmod($x, $y);
   echo "fmod(" . $x . "," . $y . ") = " . $r . "\n";
   $r=fmod($y, $x);
   echo "fmod(" . $y . "," . $x . ") = " . $r;
?>

Output

This will produce following result −

fmod(0,3.33) = 0
fmod(3.33,0) = NAN
raja
Published on 29-Jun-2020 12:23:16
Advertisements