Divide by 0 in PHP
How the division by 0 behave in PHP8, PHP7, PHP5
PHP has evolved in the last few years. Before when dividing by 0 we only get a warning. This behaviour did not change until php8, where numeric division 10 / 0
resulted in rasing an Error DivisionByZeroError. DivisionByZeroError
is thrown when an attempt is made to divide a number by zero. You have to remember that use of arithmetic operator /
does not throw an exception in php7. This does not mean that they did not try to solve it in php7, they just took a different approach. In php7 intdiv()
function were introduced which divide number and throws exception when dividing a number by 0. Before that php5 only warning were displayed.
Note that in php5 and below, you could write a custom function to throw an Exception when dividing by 0. Similar to intdiv(…) introduced in php7. It would not be a language build in function, but it would also solve this issue.
See demo for php8, php7, php5 (run the script)
<?php
echo "--- 2 / 0 ---". PHP_EOL;
try{
$res = 2 / 0;
}catch(DivisionByZeroError $e){
echo " ### Caught 2 / 0! ". $e->getMessage() . PHP_EOL;
$res = "nothing returned - error thrown!";
}
echo "RETURNS: ";
var_dump($res);
echo " ### Continue script" . PHP_EOL;
echo PHP_EOL;
echo "--- intdiv(2, 0) ---". PHP_EOL;
try{
$res = intdiv(2, 0);
}catch(DivisionByZeroError $e){
echo " ### Caught intdiv(2, 0)!". $e->getMessage() . PHP_EOL;
$res = "nothing returned - error thrown!";
}
echo "RETURNS: ";
var_dump($res);
echo " ### Continue script" . PHP_EOL;
Output – Division by 0 in PHP 8
2 / 0
### Caught 2 / 0! Division by zero
### Continue script
intdiv(2, 0)
### Caught intdiv(2, 0)! Division by zero
### Continue script
Output – Division by 0 in PHP 7
2 / 0
Warning: Division by zero in /home/user/scripts/code.php on line 5
### Continue script
intdiv(2, 0)
### Caught intdiv(2, 0)! Division by zero
### Continue script
Output – Division by 0 in PHP 5
2 / 0
Warning: Division by zero in /home/user/scripts/code.php on line 5
### Continue script
intdiv(2, 0)
Fatal error: Call to undefined function intdiv() in /home/user/scripts/code.php on line 17
<?php
/**
* Division returns INF when dividing by 0
* in PHP8, PHP7, PHP5
*
* @param int | float $a number to divide
* @param int | float $b number dividing by
* @param bool | use divint like functionality
*
* @return int | float | INF
*/
function divide($a, $b, $divint = false){
if($b == 0) return INF;
if($divint) return ($a - $a % $b) / $b;
return $a / $b;
}
/* Compatible with PHP8, PHP7, PHP5 */
divide(8,2); // returns int(4)
divide(8,1.1); // returns float(7.2727272727273)
divide(8,0); // returns INF
We could also use another approach such:
<?php
/**
* Division returns INF when dividing by 0
* in PHP8, PHP7, PHP5
*
* @param int | float $a number to divide
* @param int | float $b number dividing by
* @param bool | Division exluding the fraction
*
* @return int | float | INF
*/
function divide($a, $b, $divint = false){
if($b == 0) return INF;
if($divint) return floor(($a - $a % $b) / $b);
return $a / $b;
}
/* Compatible with PHP8, PHP7, PHP5 */
echo divide(8, 2). PHP_EOL; // returns int(4)
echo divide(8, 1.3). PHP_EOL; // returns float(7.2727272727273)
echo divide(8, 0). PHP_EOL; // returns INF