{"id":47272,"date":"2024-12-02T09:04:54","date_gmt":"2024-12-02T01:04:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47272\/"},"modified":"2024-12-02T09:04:54","modified_gmt":"2024-12-02T01:04:54","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%e6%89%80%e6%9c%89%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47272\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u6240\u6709\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u6240\u6709\u9519\u8bef\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u6240\u6709\u9519\u8bef\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p>PHP \u63d0\u4f9b error_reporting()\u3001set_error_handler() \u548c restore_error_handler() \u51fd\u6570\u5904\u7406\u4ee3\u7801\u9519\u8bef\uff1aerror_reporting() \u8bbe\u7f6e\u8981\u62a5\u544a\u7684\u9519\u8bef\u7ea7\u522b\u3002set_error_handler() \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u3002restore_error_handler() \u6062\u590d\u5230\u4ee5\u524d\u7684\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729908211671c4df308c12.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u6240\u6709\u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u6240\u6709\u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5904\u7406\u4ee3\u7801\u9519\u8bef\u7684\u6307\u5357<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8bc6\u522b\u3001\u8bca\u65ad\u548c\u5904\u7406\u8fd0\u884c\u65f6\u53d1\u751f\u7684\u9519\u8bef\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7ec4\u5185\u7f6e\u51fd\u6570\u6765\u5904\u7406\u9519\u8bef\uff0c\u5305\u62ec <code>error_reporting()<\/code>\u3001<code>set_error_handler()<\/code> \u548c <code>restore_error_handler()<\/code>\u3002<\/p>\n<p><strong>\u9519\u8bef\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<p><strong>error_reporting()<\/strong><\/p>\n<p>\u6b64\u51fd\u6570\u7528\u4e8e\u8bbe\u7f6e\u8981\u62a5\u544a\u7684\u9519\u8bef\u7ea7\u522b\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u662f\u4f4d\u63a9\u7801\uff0c\u7528\u4e8e\u6307\u793a\u8981\u5904\u7406\u7684\u9519\u8bef\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u62a5\u544a\u6240\u6709\u9519\u8bef\uff1a<\/p>\n<pre>error_reporting(E_ALL);<\/pre>\n<p><strong>set_error_handler()<\/strong><\/p>\n<p>\u6b64\u51fd\u6570\u7528\u4e8e\u8bbe\u7f6e\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u6765\u5904\u7406\u53d1\u751f\u7684\u9519\u8bef\u3002\u56de\u8c03\u51fd\u6570\u5e94\u5177\u6709\u4ee5\u4e0b\u7b7e\u540d\uff1a<\/p>\n<pre>function my_error_handler($errno, $errstr, $errfile, $errline)<\/pre>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li><code>$errno<\/code> \u662f\u9519\u8bef\u4ee3\u7801<\/li>\n<li><code>$errstr<\/code> \u662f\u9519\u8bef\u6d88\u606f<\/li>\n<li><code>$errfile<\/code> \u662f\u53d1\u751f\u9519\u8bef\u7684\u6587\u4ef6<\/li>\n<li><code>$errline<\/code> \u662f\u9519\u8bef\u53d1\u751f\u7684\u884c\u53f7<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>set_error_handler()<\/code>\uff1a<\/p>\n<pre>set_error_handler(function($errno, $errstr, $errfile, $errline) {\n  echo \"Error: $errstr in $errfile on line $errline\";\n});<\/pre>\n<p><strong>restore_error_handler()<\/strong><\/p>\n<p>\u6b64\u51fd\u6570\u7528\u4e8e\u8fd8\u539f\u5230\u4ee5\u524d\u7684\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u3002\u5b83\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>restore_error_handler()<\/code>\uff1a<\/p>\n<pre>restore_error_handler();<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4ee5\u4e0b\u573a\u666f\uff1a\u6211\u4eec\u6709\u4e00\u4e2a PHP \u811a\u672c\uff0c\u5b83\u53ef\u80fd\u4ea7\u751f\u9664\u6cd5\u9519\u8bef\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e2a\u9519\u8bef\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>set_error_handler()<\/code> \u5982\u4e0b\uff1a<\/p>\n<pre>set_error_handler(function($errno, $errstr, $errfile, $errline) {\n  if ($errno == E_DIVISION_BY_ZERO) {\n    echo \"Error: Division by zero in $errfile on line $errline\";\n  }\n});\n\n\/\/ \u89e6\u53d1\u9664\u6cd5\u9519\u8bef\n$result = 10 \/ 0;<\/pre>\n<p>\u5f53\u6211\u4eec\u8fd0\u884c\u6b64\u811a\u672c\u65f6\uff0c\u5b83\u5c06\u8f93\u51fa\u4ee5\u4e0b\u9519\u8bef\u6d88\u606f\uff1a<\/p>\n<pre>Error: Division by zero in script.php on line 10<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>PHP \u4e2d\u7684\u9519\u8bef\u5904\u7406\u51fd\u6570\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u5904\u7406\u4ee3\u7801\u4e2d\u53d1\u751f\u7684\u9519\u8bef\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8bc6\u522b\u3001\u8bca\u65ad\u548c\u5904\u7406\u9519\u8bef\uff0c\u4ece\u800c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u6240\u6709\u9519\u8bef\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u6240\u6709\u9519\u8bef\uff1f \u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60\u6587\u7ae0\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u6240\u6709\u9519\u8bef\uff1f\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01 PHP \u63d0\u4f9b error_reporting()\u3001set_error_handler() \u548c restore_error_handler() \u51fd\u6570\u5904\u7406\u4ee3\u7801\u9519\u8bef\uff1aerror_reporting() \u8bbe\u7f6e\u8981\u62a5\u544a\u7684\u9519\u8bef\u7ea7\u522b\u3002set_error_handler() \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u3002restore_error_handler() \u6062\u590d\u5230\u4ee5\u524d\u7684\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u3002 PHP \u51fd\u6570\u5904\u7406\u4ee3\u7801\u9519\u8bef\u7684\u6307\u5357 \u524d\u8a00 \u5728 PHP \u4e2d\uff0c\u9519\u8bef\u5904\u7406\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8bc6\u522b\u3001\u8bca\u65ad\u548c\u5904\u7406\u8fd0\u884c\u65f6\u53d1\u751f\u7684\u9519\u8bef\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7ec4\u5185\u7f6e\u51fd\u6570\u6765\u5904\u7406\u9519\u8bef\uff0c\u5305\u62ec error_reporting()\u3001set_error_handler() \u548c restore_error_handler()\u3002 \u9519\u8bef\u5904\u7406\u51fd\u6570 error_reporting() \u6b64\u51fd\u6570\u7528\u4e8e\u8bbe\u7f6e\u8981\u62a5\u544a\u7684\u9519\u8bef\u7ea7\u522b\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u662f\u4f4d\u63a9\u7801\uff0c\u7528\u4e8e\u6307\u793a\u8981\u5904\u7406\u7684\u9519\u8bef\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u62a5\u544a\u6240\u6709\u9519\u8bef\uff1a error_reporting(E_ALL); set_error_handler() \u6b64\u51fd\u6570\u7528\u4e8e\u8bbe\u7f6e\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u6765\u5904\u7406\u53d1\u751f\u7684\u9519\u8bef\u3002\u56de\u8c03\u51fd\u6570\u5e94\u5177\u6709\u4ee5\u4e0b\u7b7e\u540d\uff1a function my_error_handler($errno, $errstr, $errfile, $errline) \u5176\u4e2d\uff1a $errno \u662f\u9519\u8bef\u4ee3\u7801 $errstr \u662f\u9519\u8bef\u6d88\u606f $errfile \u662f\u53d1\u751f\u9519\u8bef\u7684\u6587\u4ef6 $errline \u662f\u9519\u8bef\u53d1\u751f\u7684\u884c\u53f7 \u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 set_error_handler()\uff1a set_error_handler(function($errno, $errstr, $errfile, $errline) { echo &#8220;Error: $errstr in $errfile [&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-47272","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47272","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=47272"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47272\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}