Search code examples
phpmathdivision

PHP Fatal error: Uncaught DivisionByZeroError in working by %?


I'm testing below simple code in PHP:

`<?php

echo 5 % 0.75;

?>`

And I see error division by zero whereas 0.75 is not zero!? what happens exactly?


Solution

  • The operator % is for int type and 0.75 is float. So PHP changes it to zero first :

    https://www.php.net/manual/en/language.operators.arithmetic.php

    If you want to work with float type you need to do :

    fmod(5,0.75);

    https://www.php.net/manual/en/function.fmod.php