{"record":{"id":"686d0f0e16132c74","repo":"doocs/leetcode","slug":"can-not-divide-by-0-686d0f","errorCode":null,"errorMessage":"Can not divide by 0","messagePattern":"Can not divide by 0","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"solution/0000-0099/0029.Divide Two Integers/README_EN.md","lineNumber":267,"sourceCode":"        }\n        return sign ? ans : -ans;\n    }\n}\n```\n\n#### PHP\n\n```php\nclass Solution {\n    /**\n     * @param integer $a\n     * @param integer $b\n     * @return integer\n     */\n\n    function divide($a, $b) {\n        if ($b == 0) {\n            throw new Exception('Can not divide by 0');\n        } elseif ($a == 0) {\n            return 0;\n        }\n        if ($a == -2147483648 && $b == -1) {\n            return 2147483647;\n        }\n        $sign = $a < 0 != $b < 0;\n\n        $a = abs($a);\n        $b = abs($b);\n        $ans = 0;\n        while ($a >= $b) {\n            $x = $b;\n            $cnt = 1;\n            while ($a >= $x << 1) {\n                $x <<= 1;\n                $cnt <<= 1;\n            }","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/doocs/leetcode/blob/f84f361dc403a65c4c031f6e0774297cc377f00a/solution/0000-0099/0029.Divide Two Integers/README_EN.md#L249-L285","documentation":"English README variant of the same PHP LeetCode 29 solution: divide($a, $b) throws Exception('Can not divide by 0') when $b == 0. It exists because the algorithm assumes a non-zero divisor per problem constraints and guards the boundary explicitly before performing subtraction-based division.","triggerScenarios":"Running the documented snippet with $b = 0, $b = '0', or $b = 0.0 (loose == matches all); test scripts that sweep divisors including zero.","commonSituations":"Copy-pasting the README code into a local runner and passing edge-case inputs; PHP 8 strictness changes making unguarded division fatal; assuming the overflow branch (PHP_INT_MIN / -1) is the only special case to handle.","solutions":["Guard the call site: if ($b == 0) return a sentinel or error instead of calling","Filter zero divisors out of test-case generators","Wrap in try/catch (Exception) if the value legitimately comes from untrusted input","Remember return 0 shortcut when $a == 0 — only $b == 0 throws"],"exampleFix":"// before\necho divide(7, 0); // throws 'Can not divide by 0'\n\n// after\nfunction safeDivide(int $a, int $b): ?int {\n    return $b === 0 ? null : divide($a, $b);\n}","handlingStrategy":"validation","validationCode":"$b = (int)$input; if ($b === 0) { fwrite(STDERR, \"divisor must be non-zero\\n\"); exit(1); } divide($a, $b);","typeGuard":"function isNonZeroDivisor(mixed $v): bool { return is_int($v) && $v !== 0; }","tryCatchPattern":"try { divide(7, $b); } catch (Exception $e) { /* 'Can not divide by 0' */ }","preventionTips":["Filter zero divisors from test generators","Check $b before calling","Only $b==0 throws; $a==0 short-circuits to 0"],"tags":["php","arithmetic","division-by-zero","documentation","leetcode"],"backgroundTag":"division-by-zero","analyzedSha":"f84f361dc403a65c4c031f6e0774297cc377f00a","analyzedAt":"2026-08-27T04:29:31.522Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}