Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Division by zero in PHP, as a warning and as an exception

ErrorWarning: Division by zero

Short answer

In PHP 7 the / operator warned on a zero divisor. Since PHP 8.0 it throws DivisionByZeroError, as % and intdiv() already did, so the script stops unless you catch it. Check the divisor before dividing, use fdiv() when you want INF or NAN, or catch DivisionByZeroError.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Confirm which PHP runs the code. On PHP 8 the second command prints the exception message; on PHP 7 it prints a warning. Then read the line in your stack trace and look at the divisor.

    php -v
    php -r 'try { echo 1 / 0; } catch (DivisionByZeroError $e) { echo $e->getMessage(), PHP_EOL; }'
  2. 2Match what you see, then jump to its fix

    1. 1Uncaught DivisionByZeroError: Division by zero, after upgrading from PHP 7The / operator got a zero divisor on PHP 8Fix
    2. 2DivisionByZeroError on a line that uses % or intdiv()A zero reaches % or intdiv()Fix
    3. 3Code from PHP 7 relied on INF or NAN coming back after the warningYou want IEEE results (INF or NAN), not an exceptionFix

What does the error look like?

  • Warning: Division by zero
  • Fatal error: Uncaught DivisionByZeroError: Division by zero

Cause 1 of 3

The / operator got a zero divisor on PHP 8

1Check

On PHP 7 the same code only produced a Warning and a result of INF or NAN. The change to throw DivisionByZeroError was accepted for PHP 8.0, so code that ran quietly now stops.

php -r 'try { echo 1 / 0; } catch (DivisionByZeroError $e) { echo get_class($e), ": ", $e->getMessage(), PHP_EOL; }'

2Fix

Check the divisor first and decide what a zero should mean for the page, a default value or an error message.

$average = $count > 0 ? $total / $count : 0;

3Verify

Run the code path with a zero divisor and confirm no exception is thrown.

php -r '$total = 10; $count = 0; echo $count > 0 ? $total / $count : 0, PHP_EOL;'

Confirmed by wiki.php.net

Did this fix it?

Cause 2 of 3

A zero reaches % or intdiv()

1Check

The RFC behind the PHP 8 change notes that % already threw DivisionByZeroError, and the manual says intdiv() throws it when the second argument is 0.

php -r 'try { intdiv(1, 0); } catch (Error $e) { echo get_class($e), PHP_EOL; }'

2Fix

Guard the divisor the same way, or catch the error at the point where you can respond to it. DivisionByZeroError extends Error, not Exception, so catch (Exception) will not catch it.

try {
    $pages = intdiv($items, $perPage);
} catch (DivisionByZeroError $e) {
    $pages = 0;
}

3Verify

Pass a zero page size and confirm the fallback value is used.

php -r 'try { intdiv(5, 0); } catch (DivisionByZeroError $e) { echo "caught", PHP_EOL; }'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

You want IEEE results (INF or NAN), not an exception

1Check

fdiv() is new in PHP 8 and follows IEEE 754: it returns INF, -INF or NAN for a zero divisor and does not throw.

php -r 'var_dump(fdiv(1.0, 0.0), fdiv(0.0, 0.0));'

2Fix

Use fdiv() where the old behaviour is really wanted, and test for NAN with is_nan(), since NAN never equals anything, itself included.

$ratio = fdiv($a, $b);
if (is_nan($ratio)) {
    $ratio = 0.0;
}

3Verify

Run the function with 0.0 as the divisor.

php -r 'var_dump(is_nan(fdiv(0.0, 0.0)));'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Validate numbers from forms, queries and APIs before arithmetic: empty values often become 0.
  • Run the test suite on PHP 8.x with error reporting at E_ALL so old warnings show up before release.
  • Catch DivisionByZeroError (or Error) in code that handles user-supplied numbers, and log the input.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.