Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP fmod() Function

PHP fmod() Function

fmod stands for Floating Modulo. Modulo calculates the remainder of a division which is generally noted with the ‘%’ symbol. However, the general modulo expression expects both the divisor and dividend to be of integer type, this is a great limitation to such an important operation.

In PHP the fmod() function is used to calculate the Modulo of any division which may contain floats as both dividends and divisors.

Syntax:

float fmod ($dividend, $divisor)

Parameters: The function takes two parameters as follows:

  • $dividend: This parameter takes a float which is to be divided.
  • $divisor: This parameter takes a float which is to be used as a divisor.

Return Type: This function returns a Floating-point remainder of the division.

Examples:

Input :  $dividend = 2.7,  $divisor = 1.3;   
Output : 0.1

Input : $dividend = -2.7,  $divisor = 1.1; 
Output : -0.5        

Below program illustrates the working of fmod() in PHP:




<?php
  
// PHP code to illustrate the 
// working of fmod() Function 
  
$dividend = 2.5;
$divisor = 1.1;
  
for(;$divisor<1.25;$divisor+=0.05)
    echo $dividend.' % '.$divisor.' = '.
          fmod($dividend, $divisor)."\n";
  
?>


Output:

2.5 % 1.1 = 0.3
2.5 % 1.15 = 0.2
2.5 % 1.2 = 0.1

Important points to note:

  • fmod() function returns Floating Point Modulo.
  • fmod() is an approximation method and may not always return the accurate result.

Reference:
http://php.net/manual/en/function.fmod.php

RELATED ARTICLES

Most Popular

Recent Comments