{"id":46760,"date":"2024-12-02T10:51:07","date_gmt":"2024-12-02T02:51:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46760\/"},"modified":"2024-12-02T10:51:07","modified_gmt":"2024-12-02T02:51:07","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e4%bb%a3%e7%a0%81%e4%b8%ad%e7%89%b9%e5%ae%9a%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46760\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u7279\u5b9a\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u7279\u5b9a\u9519\u8bef\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u7279\u5b9a\u9519\u8bef\uff1f\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p>PHP \u51fd\u6570\u4e2d\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u5206\u4e3a\u81f4\u547d\u9519\u8bef\u548c\u53ef\u6062\u590d\u9519\u8bef\uff0c\u901a\u8fc7\u5185\u90e8\u6216\u5916\u90e8\u65b9\u5f0f\u62a5\u544a\u9519\u8bef\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 set_error_handler() \u51fd\u6570\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u5904\u7406 PHP \u9519\u8bef\u4ee3\u7801\u548c\u6d88\u606f\uff0c\u5e76\u4ee5\u66f4\u6709\u7528\u7684\u65b9\u5f0f\u663e\u793a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729844851671b567375303.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u7279\u5b9a\u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u7279\u5b9a\u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u4e2d\u7684\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<p><strong>\u6982\u8ff0<\/strong><\/p>\n<p>PHP \u51fd\u6570\u4f7f\u7528\u9519\u8bef\u5904\u7406\u673a\u5236\u6765\u62a5\u544a\u95ee\u9898\uff0c\u4ece\u800c\u5e2e\u52a9\u60a8\u8bc6\u522b\u548c\u4fee\u590d\u4ee3\u7801\u4e2d\u7684 bug\u3002<\/p>\n<p><strong>\u9519\u8bef\u7c7b\u578b<\/strong><\/p>\n<p>PHP \u6709\u4e24\u79cd\u4e3b\u8981\u9519\u8bef\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li><strong>\u81f4\u547d\u9519\u8bef (Fatal Error)\uff1a<\/strong>\u8fd9\u4e9b\u9519\u8bef\u4f1a\u7acb\u5373\u7ec8\u6b62\u811a\u672c\u6267\u884c\u3002<\/li>\n<li><strong>\u53ef\u6062\u590d\u9519\u8bef (Recoverable Error)\uff1a<\/strong>\u8fd9\u4e9b\u9519\u8bef\u4e0d\u4f1a\u7ec8\u6b62\u811a\u672c\u6267\u884c\uff0c\u4f46\u4f1a\u5f71\u54cd\u811a\u672c\u7684\u6b63\u5e38\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u9519\u8bef\u62a5\u544a<\/strong><\/p>\n<p>PHP \u51fd\u6570\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u62a5\u544a\u9519\u8bef\uff1a<\/p>\n<ul>\n<li><strong>\u5185\u90e8 &#8211;<\/strong> \u9519\u8bef\u6d88\u606f\u76f4\u63a5\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa\uff08\u901a\u5e38\u662f\u6d4f\u89c8\u5668\uff09\u3002<\/li>\n<li><strong>\u5916\u90e8 &#8211;<\/strong> \u9519\u8bef\u6d88\u606f\u901a\u8fc7\u6307\u5b9a\u7684\u9519\u8bef\u5904\u7406\u51fd\u6570\u53d1\u9001\u3002<\/li>\n<\/ul>\n<p><strong>\u8bbe\u7f6e\u9519\u8bef\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>set_error_handler()<\/code> \u51fd\u6570\u6307\u5b9a\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002\u8be5\u51fd\u6570\u5fc5\u987b\u63a5\u53d7\u4e00\u4e2a PHP \u9519\u8bef\u4ee3\u7801\u548c\u6d88\u606f\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<pre>function my_error_handler($error_code, $error_message) {\n  \/\/ \u5904\u7406\u9519\u8bef...\n}\n\nset_error_handler(\"my_error_handler\");<\/pre>\n<p><strong>\u5b9e\u6218<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a<\/p>\n<pre>function divide($num1, $num2) {\n  if ($num2 == 0) {\n    trigger_error(\"\u65e0\u6cd5\u9664\u4ee5 0\", E_USER_ERROR);\n  }\n\n  return $num1 \/ $num2;\n}<\/pre>\n<p>\u6b64\u51fd\u6570\u5728\u88ab 0 \u9664\u65f6\u4f1a\u89e6\u53d1\u4e00\u4e2a\u81f4\u547d\u9519\u8bef\u3002\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u9519\u8bef\u5904\u7406\u51fd\u6570\u6765\u4ee5\u66f4\u6709\u7528\u7684\u65b9\u5f0f\u5904\u7406\u8be5\u9519\u8bef\uff1a<\/p>\n<pre>set_error_handler(\"my_error_handler\");\n\nfunction my_error_handler($error_code, $error_message) {\n  if ($error_code == E_USER_ERROR) {\n    echo \"\u9664\u6570\u4e0d\u80fd\u4e3a 0\";\n  } else {\n    echo \"\u672a\u77e5\u9519\u8bef\";\n  }\n}\n\ndivide(10, 2); \/\/ \u8f93\u51fa 5\ndivide(10, 0); \/\/ \u8f93\u51fa \"\u9664\u6570\u4e0d\u80fd\u4e3a 0\"<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8ephp,\u9519\u8bef\u5904\u7406\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u7279\u5b9a\u9519\u8bef\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u7279\u5b9a\u9519\u8bef\uff1f \u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aPHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u7279\u5b9a\u9519\u8bef\uff1f\u300b\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9\u6587\u7ae0\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01 PHP \u51fd\u6570\u4e2d\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u5206\u4e3a\u81f4\u547d\u9519\u8bef\u548c\u53ef\u6062\u590d\u9519\u8bef\uff0c\u901a\u8fc7\u5185\u90e8\u6216\u5916\u90e8\u65b9\u5f0f\u62a5\u544a\u9519\u8bef\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 set_error_handler() \u51fd\u6570\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u5904\u7406 PHP \u9519\u8bef\u4ee3\u7801\u548c\u6d88\u606f\uff0c\u5e76\u4ee5\u66f4\u6709\u7528\u7684\u65b9\u5f0f\u663e\u793a\u9519\u8bef\u4fe1\u606f\u3002 PHP \u51fd\u6570\u4e2d\u7684\u9519\u8bef\u5904\u7406 \u6982\u8ff0 PHP \u51fd\u6570\u4f7f\u7528\u9519\u8bef\u5904\u7406\u673a\u5236\u6765\u62a5\u544a\u95ee\u9898\uff0c\u4ece\u800c\u5e2e\u52a9\u60a8\u8bc6\u522b\u548c\u4fee\u590d\u4ee3\u7801\u4e2d\u7684 bug\u3002 \u9519\u8bef\u7c7b\u578b PHP \u6709\u4e24\u79cd\u4e3b\u8981\u9519\u8bef\u7c7b\u578b\uff1a \u81f4\u547d\u9519\u8bef (Fatal Error)\uff1a\u8fd9\u4e9b\u9519\u8bef\u4f1a\u7acb\u5373\u7ec8\u6b62\u811a\u672c\u6267\u884c\u3002 \u53ef\u6062\u590d\u9519\u8bef (Recoverable Error)\uff1a\u8fd9\u4e9b\u9519\u8bef\u4e0d\u4f1a\u7ec8\u6b62\u811a\u672c\u6267\u884c\uff0c\u4f46\u4f1a\u5f71\u54cd\u811a\u672c\u7684\u6b63\u5e38\u8fd0\u884c\u3002 \u9519\u8bef\u62a5\u544a PHP \u51fd\u6570\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u62a5\u544a\u9519\u8bef\uff1a \u5185\u90e8 &#8211; \u9519\u8bef\u6d88\u606f\u76f4\u63a5\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa\uff08\u901a\u5e38\u662f\u6d4f\u89c8\u5668\uff09\u3002 \u5916\u90e8 &#8211; \u9519\u8bef\u6d88\u606f\u901a\u8fc7\u6307\u5b9a\u7684\u9519\u8bef\u5904\u7406\u51fd\u6570\u53d1\u9001\u3002 \u8bbe\u7f6e\u9519\u8bef\u5904\u7406\u51fd\u6570 \u60a8\u53ef\u4ee5\u4f7f\u7528 set_error_handler() \u51fd\u6570\u6307\u5b9a\u4e00\u4e2a\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002\u8be5\u51fd\u6570\u5fc5\u987b\u63a5\u53d7\u4e00\u4e2a PHP \u9519\u8bef\u4ee3\u7801\u548c\u6d88\u606f\u4f5c\u4e3a\u53c2\u6570\u3002 function my_error_handler($error_code, $error_message) { \/\/ \u5904\u7406\u9519\u8bef&#8230; } set_error_handler(&#8220;my_error_handler&#8221;); \u5b9e\u6218 \u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff1a function divide($num1, $num2) { if [&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-46760","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46760","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=46760"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46760\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}