{"record":{"id":"0fc603d91e1bfb4c","repo":"doocs/leetcode","slug":"can-not-divide-by-0","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.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.md#L249-L285","documentation":"PHP solution for LeetCode 29 (Divide Two Integers) explicitly throws Exception('Can not divide by 0') when the divisor $b is 0, because the problem guarantees non-zero divisors but a robust guard is included. PHP itself would throw a DivisionByZeroError for the modulo/intdiv path anyway; this guard gives a controlled message and also handles the $a == -2147483648 && $b == -1 overflow case.","triggerScenarios":"Calling divide($a, 0) directly, or with $b arriving from user input/configuration that evaluates to 0 (including '0' or 0.0 due to PHP's loose ==).","commonSituations":"Test harnesses passing 0 as the divisor; values parsed from request parameters defaulting to 0; porting the algorithm where callers assume the constraint is enforced upstream; PHP 8 environments where the engine's own DivisionByZeroError would otherwise surface.","solutions":["Validate the divisor before calling: if ($b == 0) handle gracefully (return 0, null, or an error) ","Check where $b originates (query param, config) and reject/filter zero early","Catch Exception around the call only as a last-resort safety net","Note the special case: divide(PHP_INT_MIN, -1) returns PHP_INT_MAX by design"],"exampleFix":"// before\ndivide(10, (int) $_GET['b']); // throws when b=0\n\n// after\n$b = (int) $_GET['b'];\nif ($b === 0) { http_response_code(400); exit('divisor must be non-zero'); }\ndivide(10, $b);","handlingStrategy":"validation","validationCode":"if ($b == 0) { return null; /* or throw a domain-specific error */ }\nreturn divide($a, $b);","typeGuard":"function isNonZeroInt($v): bool { return is_numeric($v) && (int)$v !== 0; }","tryCatchPattern":"try { divide($a, $b); } catch (Exception $e) { if ($e->getMessage() === 'Can not divide by 0') { /* handle */ } else throw $e; }","preventionTips":["Validate divisor at input boundaries","Cast and check request parameters","Handle PHP_INT_MIN / -1 overflow separately"],"tags":["php","arithmetic","division-by-zero","leetcode"],"backgroundTag":"division-by-zero","analyzedSha":"f84f361dc403a65c4c031f6e0774297cc377f00a","analyzedAt":"2026-08-27T04:29:31.522Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}