Ad Space — Top Banner

Warning: Division by zero

PHP Programming Language

Severity: Moderate

What Does This Error Mean?

This warning appears when your PHP code tries to divide a number by zero. Mathematically, division by zero is undefined — so PHP cannot produce a result. In PHP 8, dividing by zero returns a warning. In PHP 7 and earlier, it could return false or trigger a fatal error depending on the context. The fix is to check that your divisor is not zero before dividing.

Affected Models

  • PHP 5.x
  • PHP 7.x
  • PHP 8.x
  • All PHP versions

Common Causes

  • A variable used as a divisor is zero because it was not set or defaulted to zero
  • A database query returned zero or null and the result was used in a division
  • User input was empty or zero and was not validated before being used in a calculation
  • A percentage or average calculation divides by a total that can be zero when there is no data
  • A loop counter or index was used as a divisor without checking if it had reached zero

How to Fix It

  1. Before dividing, check if the divisor is zero. Use: if ($divisor != 0) { $result = $numerator / $divisor; }

    This is the most reliable fix. Never divide without checking the divisor first.

  2. If zero is a valid result when the divisor is zero, use a ternary shortcut: $result = $divisor != 0 ? $numerator / $divisor : 0;

    This returns 0 instead of crashing when the divisor is zero.

  3. Validate user input before using it in calculations. Cast it to a number and check its value.

    Use intval() or floatval() to convert input, then check if the result is zero.

  4. When dividing by a database value, always check for null or zero in your query result before calculating.

    A COUNT or SUM query can return zero if there are no matching rows.

  5. In PHP 8, you can use the intdiv() function for integer division — it throws a DivisionByZeroError instead of a warning, which you can catch with try/catch.

    try { $result = intdiv($a, $b); } catch (DivisionByZeroError $e) { $result = 0; }

When to Call a Professional

Division by zero warnings rarely need professional help. They are almost always fixed by adding a simple check before the division. If the warning appears in a library or framework you did not write, report it as a bug to that project.

Frequently Asked Questions

Is division by zero a fatal error or just a warning in PHP?

In PHP 8, dividing by zero with the / operator gives a warning and returns false. Using intdiv() with zero throws a DivisionByZeroError, which is more serious. In PHP 7, it was a warning that returned false. Either way, your script continues running — but the result is wrong.

What does PHP return when you divide by zero?

PHP returns false when using the / operator with a zero divisor. This can cause unexpected behavior if you then use that result in more calculations. Always check the divisor before dividing.

Can I turn off the division by zero warning?

Yes, but you should not. Suppressing warnings hides real problems in your code. Fix the root cause instead — add a check before the division. Suppressing errors makes debugging much harder later.