When an integer number is divided by another integer number, output will be in integer with some decimal values in PHP.
3 ways of removing decimal value with integer division.
- round()
- floor()
- intdiv()
For example, when an integer 7 is divided by 2, output will be 3.5. Decimals can be removed with PHP built in functions round() or float() as shown below.
// Usage of round function rounds it to upper integer value
round ( 7/2 ) ; // gives output 4
// Usage of floor function gives the correct integer without decimals
floor ( 7/2 ) ; // gives output 3
PHP 7 supports new built in function intdiv() which does same as division followed by floor() function. With the introduction of intdiv function, using floor() is not required in PHP7.
// intdiv() function in PHP7 is equivalent of integer division followed by floor() function in earlier
versions of PHP.
intdiv ( 7 , 2 ) ; // gives output 3
Please keep an eye on the syntax of intdiv ( dividend , divisor ). It requires to use comma in place of division character.