{"id":47092,"date":"2024-12-02T16:52:07","date_gmt":"2024-12-02T08:52:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47092\/"},"modified":"2024-12-02T16:52:07","modified_gmt":"2024-12-02T08:52: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%e5%b8%b8%e8%a7%81%e7%9a%84%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47092\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\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\u5e38\u89c1\u7684\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\u4ee5\u4e0b\u51fd\u6570\u5904\u7406\u4ee3\u7801\u5e38\u89c1\u9519\u8bef\uff1aerror_reporting(): \u63a7\u5236\u54ea\u4e9b\u9519\u8bef\u62a5\u544a\u3002set_error_handler(): \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002trigger_error(): \u89e6\u53d1\u81ea\u5b9a\u4e49\u9519\u8bef\u3002PHP \u8fd8\u63d0\u4f9b\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u5305\u62ec\uff1aset_exception_handler(): \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u51fd\u6570\u3002throw new Exception(): \u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002\u5229\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u68c0\u6d4b\u3001\u5904\u7406\u548c\u8bb0\u5f55\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729865733671ba8058d12b.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\uff1f<\/strong><\/p>\n<p><strong>\u4ecb\u7ecd<\/strong><\/p>\n<p>PHP \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u51fd\u6570\u6765\u68c0\u6d4b\u548c\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u672c\u6587\u5c06\u63a2\u7a76\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u6f14\u793a\u5b83\u4eec\u7684\u7528\u6cd5\u3002<\/p>\n<p><strong>\u9519\u8bef\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406\u9519\u8bef\u7684\u5e38\u7528 PHP \u51fd\u6570\uff1a<\/p>\n<ul>\n<li><code>error_reporting()<\/code>: \u63a7\u5236\u54ea\u4e9b\u9519\u8bef\u62a5\u544a\u3002<\/li>\n<li><code>set_error_handler()<\/code>: \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002<\/li>\n<li><code>trigger_error()<\/code>: \u89e6\u53d1\u81ea\u5b9a\u4e49\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570 <code>calculateAverage()<\/code>\uff0c\u8be5\u51fd\u6570\u8ba1\u7b97\u4e00\u7ec4\u6570\u5b57\u7684\u5e73\u5747\u503c\u3002\u5982\u679c\u8f93\u5165\u6570\u5b57\u4e0d\u6b63\u786e\uff0c\u5219\u6211\u4eec\u5e0c\u671b\u663e\u793a\u81ea\u5b9a\u4e49\u9519\u8bef\u6d88\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>set_error_handler()<\/code> \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff1a<\/p>\n<pre>function customErrorHandler($errno, $errstr, $errfile, $errline) {\n  \/\/ \u5904\u7406\u9519\u8bef\uff0c\u4f8b\u5982\u663e\u793a\u81ea\u5b9a\u4e49\u9519\u8bef\u6d88\u606f\u6216\u8bb0\u5f55\u5230\u65e5\u5fd7\u4e2d\n}\n\nset_error_handler('customErrorHandler');\n\n\/\/ \u8c03\u7528\u8ba1\u7b97\u5e73\u5747\u503c\u7684\u51fd\u6570\n$average = calculateAverage([1, 2, 3, 'invalid']);<\/pre>\n<p>\u5982\u679c <code>calculateAverage()<\/code> \u51fd\u6570\u9047\u5230\u9519\u8bef\uff08\u4f8b\u5982\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff09\uff0c\u5219 <code>customErrorHandler()<\/code> \u51fd\u6570\u5c06\u88ab\u8c03\u7528\uff0c\u5e76\u4e14\u53ef\u4ee5\u5904\u7406\u9519\u8bef\u3002<\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<p>PHP \u8fd8\u63d0\u4f9b\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u5904\u7406\u66f4\u4e25\u91cd\u7684\u9519\u8bef\uff1a<\/p>\n<ul>\n<li><code>set_exception_handler()<\/code>: \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u51fd\u6570\u3002<\/li>\n<li><code>throw new Exception()<\/code>: \u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u629b\u51fa\u5f02\u5e38<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570 <code>validateUserData()<\/code>\uff0c\u8be5\u51fd\u6570\u9a8c\u8bc1\u7528\u6237\u6570\u636e\u662f\u5426\u6709\u6548\u3002\u5982\u679c\u6570\u636e\u65e0\u6548\uff0c\u5219\u6211\u4eec\u5e0c\u671b\u629b\u51fa\u5f02\u5e38\uff1a<\/p>\n<pre>function validateUserData($data) {\n  \/\/ \u6570\u636e\u9a8c\u8bc1\u903b\u8f91\n  if (!is_string($data) || empty($data)) {\n    throw new Exception('Invalid user data');\n  }\n}\n\n\/\/ \u6355\u6349\u5e76\u5904\u7406\u5f02\u5e38\ntry {\n  validateUserData('John Doe');\n} catch (Exception $e) {\n  \/\/ \u5904\u7406\u5f02\u5e38\uff0c\u4f8b\u5982\u663e\u793a\u9519\u8bef\u6d88\u606f\u6216\u8bb0\u5f55\u5230\u6570\u636e\u5e93\u4e2d\n}<\/pre>\n<p>\u5982\u679c <code>validateUserData()<\/code> \u51fd\u6570\u68c0\u6d4b\u5230\u65e0\u6548\u6570\u636e\uff0c\u5219\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002\u8be5\u5f02\u5e38\u5728 <code>try-catch<\/code> \u5757\u4e2d\u88ab\u6355\u6349\u5e76\u5904\u7406\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>PHP \u7684\u9519\u8bef\u548c\u5f02\u5e38\u5904\u7406\u51fd\u6570\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u673a\u5236\u6765\u68c0\u6d4b\u3001\u5904\u7406\u548c\u8bb0\u5f55\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\u548c\u5f02\u5e38\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\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\u5e38\u89c1\u7684\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\u4ee5\u4e0b\u51fd\u6570\u5904\u7406\u4ee3\u7801\u5e38\u89c1\u9519\u8bef\uff1aerror_reporting(): \u63a7\u5236\u54ea\u4e9b\u9519\u8bef\u62a5\u544a\u3002set_error_handler(): \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002trigger_error(): \u89e6\u53d1\u81ea\u5b9a\u4e49\u9519\u8bef\u3002PHP \u8fd8\u63d0\u4f9b\u5f02\u5e38\u5904\u7406\u51fd\u6570\uff0c\u5305\u62ec\uff1aset_exception_handler(): \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u51fd\u6570\u3002throw new Exception(): \u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002\u5229\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u68c0\u6d4b\u3001\u5904\u7406\u548c\u8bb0\u5f55\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002 PHP \u51fd\u6570\u5982\u4f55\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\uff1f \u4ecb\u7ecd PHP \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u51fd\u6570\u6765\u68c0\u6d4b\u548c\u5904\u7406\u4ee3\u7801\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u672c\u6587\u5c06\u63a2\u7a76\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\u6f14\u793a\u5b83\u4eec\u7684\u7528\u6cd5\u3002 \u9519\u8bef\u5904\u7406\u51fd\u6570 \u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406\u9519\u8bef\u7684\u5e38\u7528 PHP \u51fd\u6570\uff1a error_reporting(): \u63a7\u5236\u54ea\u4e9b\u9519\u8bef\u62a5\u544a\u3002 set_error_handler(): \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002 trigger_error(): \u89e6\u53d1\u81ea\u5b9a\u4e49\u9519\u8bef\u3002 \u5b9e\u6218\u6848\u4f8b\uff1a\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570 \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570 calculateAverage()\uff0c\u8be5\u51fd\u6570\u8ba1\u7b97\u4e00\u7ec4\u6570\u5b57\u7684\u5e73\u5747\u503c\u3002\u5982\u679c\u8f93\u5165\u6570\u5b57\u4e0d\u6b63\u786e\uff0c\u5219\u6211\u4eec\u5e0c\u671b\u663e\u793a\u81ea\u5b9a\u4e49\u9519\u8bef\u6d88\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 set_error_handler() \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u51fd\u6570\uff1a function customErrorHandler($errno, $errstr, $errfile, $errline) { \/\/ \u5904\u7406\u9519\u8bef\uff0c\u4f8b\u5982\u663e\u793a\u81ea\u5b9a\u4e49\u9519\u8bef\u6d88\u606f\u6216\u8bb0\u5f55\u5230\u65e5\u5fd7\u4e2d } set_error_handler(&#8216;customErrorHandler&#8217;); \/\/ \u8c03\u7528\u8ba1\u7b97\u5e73\u5747\u503c\u7684\u51fd\u6570 $average = calculateAverage([1, 2, 3, &#8216;invalid&#8217;]); \u5982\u679c calculateAverage() \u51fd\u6570\u9047\u5230\u9519\u8bef\uff08\u4f8b\u5982\u5c1d\u8bd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff09\uff0c\u5219 [&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-47092","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47092","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=47092"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47092\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}