What is the best function or method for computing modulus (Remainder of $a divided by $b) in PHP?
modulus, numbers, php
Solution
Only the modulus operator (%), and fmod are native
The modulus operator cannot handle numbers beyond `2^32` (on PHP running x86 architecture)
fmod runs faster than bcmod/gmp_mod ~benchmark
bcmod doesn't work with floats ~here
I believe it is best to use `fmod`, simply because it's within Math Functions, runs way faster than other function, and most importantly, can handle large numbers and floats.
If you don't plan on using numbers past the limit or floats, use `%` as it should be the fastest.
Problem
PHP provides different functions for computing modulus: - Modulus operator `%`: `echo ($a % $b);` - fmod() - bcmod() - gmp_mod() Which function should be used that considered safe and efficient? considering the (devision by zero issue that described here: Division by zero error while using modulus and floating numbers?