{"id":47048,"date":"2024-12-02T09:02:54","date_gmt":"2024-12-02T01:02:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47048\/"},"modified":"2024-12-02T09:02:54","modified_gmt":"2024-12-02T01:02:54","slug":"%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86php%e5%87%bd%e6%95%b0%e5%bc%95%e5%8f%91%e7%9a%84%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47048\/","title":{"rendered":"\u5982\u4f55\u5904\u7406php\u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5904\u7406php\u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef<\/h1>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5904\u7406php\u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p>PHP \u51fd\u6570\u5e38\u5f15\u53d1\u7684\u9519\u8bef\u53ef\u5206\u4e3a\u8bed\u6cd5\u3001\u81f4\u547d\u3001\u8b66\u544a\u548c\u6ce8\u610f\u56db\u79cd\u7c7b\u578b\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6280\u672f\u5904\u7406\uff1a1. \u4f7f\u7528 set_error_handler() \u5b9a\u4e49\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff1b2. \u8bbe\u7f6e\u9519\u8bef\u62a5\u544a\u7ea7\u522b\u4ee5\u6307\u5b9a\u5e94\u62a5\u544a\u7684\u9519\u8bef\u7c7b\u578b\uff1b3. \u4f7f\u7528 try&#8230;catch \u5757\u4fdd\u62a4\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\u7684\u4ee3\u7801\u5757\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u4f7f\u7528 try&#8230;catch \u5757\u5904\u7406 divide() \u51fd\u6570\u5f15\u53d1\u7684\u9664\u96f6\u9519\u8bef\uff0c\u751f\u6210\u53cb\u597d\u4e14\u6709\u7528\u7684\u9519\u8bef\u6d88\u606f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729862015671b997fd2fb2.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5904\u7406php\u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef\u63d2\u56fe\" alt=\"\u5982\u4f55\u5904\u7406php\u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5904\u7406 PHP \u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>PHP \u51fd\u6570\u5728\u5904\u7406\u6570\u636e\u548c\u6267\u884c\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f46\u6709\u65f6\u5b83\u4eec\u4e5f\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u53ca\u65f6\u4fee\u590d\u95ee\u9898\u5e76\u9632\u6b62\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<p><strong>\u5e38\u89c1\u9519\u8bef\u7c7b\u578b<\/strong><\/p>\n<p>PHP \u51fd\u6570\u53ef\u80fd\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef\u7c7b\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u8bed\u6cd5\u9519\u8bef\uff1a<\/strong>\u4ee3\u7801\u4e2d\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u65f6\uff08\u4f8b\u5982\u672a\u95ed\u5408\u7684\u62ec\u53f7\uff09\u3002<\/li>\n<li><strong>\u81f4\u547d\u9519\u8bef\uff1a<\/strong>\u4e25\u91cd\u9519\u8bef\uff0c\u4f1a\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u7acb\u5373\u505c\u6b62\u6267\u884c\u3002<\/li>\n<li><strong>\u8b66\u544a\uff1a<\/strong>\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u4e0d\u4e00\u5b9a\u4f1a\u963b\u6b62\u5e94\u7528\u7a0b\u5e8f\u6267\u884c\uff0c\u4f46\u5e94\u4e88\u4ee5\u6ce8\u610f\u3002<\/li>\n<li><strong>\u6ce8\u610f\uff1a<\/strong>\u901a\u77e5\u6216\u5efa\u8bae\uff0c\u901a\u5e38\u4e0d\u8868\u793a\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><strong>\u9519\u8bef\u5904\u7406\u6280\u672f<\/strong><\/p>\n<p>PHP \u63d0\u4f9b\u4e86\u51e0\u79cd\u6280\u672f\u6765\u5904\u7406\u51fd\u6570\u5f15\u53d1\u7684\u9519\u8bef\uff1a<\/p>\n<p><strong>1. set_error_handler() \u51fd\u6570<\/strong><\/p>\n<p>\u6b64\u51fd\u6570\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u53d1\u751f\u9519\u8bef\u65f6\u88ab\u8c03\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>set_error_handler(function(int $errno, string $errstr, string $errfile, int $errline, array $errcontext) {\n    \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u9519\u8bef\n});<\/pre>\n<p><strong>2. \u9519\u8bef\u5e38\u91cf\u548c error_reporting() \u51fd\u6570<\/strong><\/p>\n<p>PHP \u5b9a\u4e49\u4e86\u9519\u8bef\u5e38\u91cf\uff08\u4f8b\u5982 E_ERROR \u548c E_WARNING\uff09\u6765\u8868\u793a\u4e0d\u540c\u7684\u9519\u8bef\u7c7b\u578b\u3002error_reporting() \u51fd\u6570\u53ef\u4ee5\u8bbe\u7f6e\u9519\u8bef\u62a5\u544a\u7ea7\u522b\uff0c\u6307\u5b9a\u5c06\u62a5\u544a\u54ea\u4e9b\u7c7b\u578b\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>error_reporting(E_ALL); \/\/ \u62a5\u544a\u6240\u6709\u9519\u8bef<\/pre>\n<p><strong>3. try&#8230;catch \u5757<\/strong><\/p>\n<p>try&#8230;catch \u5757\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\u7684\u4ee3\u7801\u5757\u5468\u56f4\u653e\u7f6e\u4e00\u4e2a\u4fdd\u62a4\u5757\u3002\u5982\u679c\u53d1\u751f\u9519\u8bef\uff0c\u5c06\u6267\u884c catch \u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>try {\n    $result = divide(10, 0);\n} catch (DivisionByZeroError $e) {\n    \/\/ \u5904\u7406\u9664\u96f6\u9519\u8bef\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 try&#8230;catch \u5757\u5904\u7406 PHP \u51fd\u6570\u5f15\u53d1\u7684\u9519\u8bef\u7684\u5b9e\u9645\u4f8b\u5b50\uff1a<\/p>\n<pre>function divide($dividend, $divisor) {\n    if ($divisor == 0) {\n        throw new DivisionByZeroError(\"\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\");\n    }\n    return $dividend \/ $divisor;\n}\n\ntry {\n    $result = divide(10, 2);\n    echo \"\u7ed3\u679c\uff1a{$result}\";\n} catch (DivisionByZeroError $e) {\n    echo \"\u9519\u8bef\uff1a{$e-&gt;getMessage()}\";\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cdivide() \u51fd\u6570\u901a\u8fc7 throw \u8bed\u53e5\u5f15\u53d1 DivisionByZeroError \u5f02\u5e38\u3002try&#8230;catch \u5757\u6355\u83b7\u8be5\u5f02\u5e38\u5e76\u5904\u7406\u9519\u8bef\uff0c\u751f\u6210\u53cb\u597d\u4e14\u6709\u7528\u7684\u9519\u8bef\u6d88\u606f\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5904\u7406php\u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5e38\u89c1\u9519\u8bef,PHP\u51fd\u6570\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5904\u7406php\u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef \u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u5982\u4f55\u5904\u7406php\u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef\u300b\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u6216\u8005\u662f\u5bf9\u6587\u7ae0\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01 PHP \u51fd\u6570\u5e38\u5f15\u53d1\u7684\u9519\u8bef\u53ef\u5206\u4e3a\u8bed\u6cd5\u3001\u81f4\u547d\u3001\u8b66\u544a\u548c\u6ce8\u610f\u56db\u79cd\u7c7b\u578b\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6280\u672f\u5904\u7406\uff1a1. \u4f7f\u7528 set_error_handler() \u5b9a\u4e49\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff1b2. \u8bbe\u7f6e\u9519\u8bef\u62a5\u544a\u7ea7\u522b\u4ee5\u6307\u5b9a\u5e94\u62a5\u544a\u7684\u9519\u8bef\u7c7b\u578b\uff1b3. \u4f7f\u7528 try&#8230;catch \u5757\u4fdd\u62a4\u53ef\u80fd\u5f15\u53d1\u9519\u8bef\u7684\u4ee3\u7801\u5757\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u4f7f\u7528 try&#8230;catch \u5757\u5904\u7406 divide() \u51fd\u6570\u5f15\u53d1\u7684\u9664\u96f6\u9519\u8bef\uff0c\u751f\u6210\u53cb\u597d\u4e14\u6709\u7528\u7684\u9519\u8bef\u6d88\u606f\u3002 \u5982\u4f55\u5904\u7406 PHP \u51fd\u6570\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef \u5f15\u8a00 PHP \u51fd\u6570\u5728\u5904\u7406\u6570\u636e\u548c\u6267\u884c\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f46\u6709\u65f6\u5b83\u4eec\u4e5f\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u53ca\u65f6\u4fee\u590d\u95ee\u9898\u5e76\u9632\u6b62\u5e94\u7528\u7a0b\u5e8f\u5d29\u6e83\u3002 \u5e38\u89c1\u9519\u8bef\u7c7b\u578b PHP \u51fd\u6570\u53ef\u80fd\u5f15\u53d1\u7684\u5e38\u89c1\u9519\u8bef\u7c7b\u578b\u5305\u62ec\uff1a \u8bed\u6cd5\u9519\u8bef\uff1a\u4ee3\u7801\u4e2d\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u65f6\uff08\u4f8b\u5982\u672a\u95ed\u5408\u7684\u62ec\u53f7\uff09\u3002 \u81f4\u547d\u9519\u8bef\uff1a\u4e25\u91cd\u9519\u8bef\uff0c\u4f1a\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u7acb\u5373\u505c\u6b62\u6267\u884c\u3002 \u8b66\u544a\uff1a\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u4e0d\u4e00\u5b9a\u4f1a\u963b\u6b62\u5e94\u7528\u7a0b\u5e8f\u6267\u884c\uff0c\u4f46\u5e94\u4e88\u4ee5\u6ce8\u610f\u3002 \u6ce8\u610f\uff1a\u901a\u77e5\u6216\u5efa\u8bae\uff0c\u901a\u5e38\u4e0d\u8868\u793a\u9519\u8bef\u3002 \u9519\u8bef\u5904\u7406\u6280\u672f PHP \u63d0\u4f9b\u4e86\u51e0\u79cd\u6280\u672f\u6765\u5904\u7406\u51fd\u6570\u5f15\u53d1\u7684\u9519\u8bef\uff1a 1. set_error_handler() \u51fd\u6570 \u6b64\u51fd\u6570\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u53d1\u751f\u9519\u8bef\u65f6\u88ab\u8c03\u7528\u3002\u4f8b\u5982\uff1a set_error_handler(function(int $errno, string $errstr, string $errfile, int $errline, array $errcontext) { \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u9519\u8bef }); 2. \u9519\u8bef\u5e38\u91cf\u548c error_reporting() \u51fd\u6570 PHP \u5b9a\u4e49\u4e86\u9519\u8bef\u5e38\u91cf\uff08\u4f8b\u5982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47048","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47048"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47048\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}