{"id":47128,"date":"2024-12-02T12:12:10","date_gmt":"2024-12-02T04:12:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47128\/"},"modified":"2024-12-02T12:12:10","modified_gmt":"2024-12-02T04:12:10","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%8e%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47128\/","title":{"rendered":"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316\u4e00\u3001\u5f02\u5e38\u5904\u7406\u5f02\u5e38\u5904\u7406\u673a\u5236\u5141\u8bb8\u4f18\u96c5\u5730\u6355\u83b7\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u901a\u8fc7 try&#8230;catch \u5757\u6355\u83b7 Throwable \u7c7b\u7684\u5b50\u7c7b\u5f02\u5e38\u3002\u4e8c\u3001\u6027\u80fd\u4f18\u5316\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u54c8\u5e0c\u8868\uff09\u3002\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u3002\u7f13\u5b58\u7ed3\u679c\u3002\u4f7f\u7528 Composer \u81ea\u52a8\u52a0\u8f7d\u5668\u3002\u5b9e\u6218\u6848\u4f8b\u5f02\u5e38\u5904\u7406\uff1a\u9664\u6570\u4e3a 0 \u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u63d0\u4f9b\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u3002\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528\u6570\u7ec4\u67e5\u627e\u6bd4\u5faa\u73af\u67e5\u627e\u66f4\u5feb\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729867465671baec939dfd.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u8ba9\u5f00\u53d1\u4eba\u5458\u4f18\u96c5\u5730\u6355\u83b7\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u662f <code>Throwable<\/code> \u7c7b\u7684\u5b50\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>try...catch<\/code> \u5757\u6765\u6355\u83b7\u5b83\u4eec\u3002<\/p>\n<pre>&lt;?php\n\ntry {\n    throw new Exception('\u9519\u8bef\u4fe1\u606f');\n} catch (Exception $e) {\n    \/\/ \u5904\u7406\u9519\u8bef\n}<\/pre>\n<p>\u901a\u8fc7\u6355\u83b7\u5f02\u5e38\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u63d0\u4f9b\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u6216\u91c7\u53d6\u9002\u5f53\u7684\u8865\u6551\u63aa\u65bd\u3002<\/p>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<p>PHP \u6027\u80fd\u4f18\u5316\u6d89\u53ca\u5230\u6d88\u9664\u74f6\u9888\u548c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u4f18\u5316\u7b56\u7565\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff1a<\/strong>\u6570\u7ec4\u6bd4\u94fe\u8868\u5177\u6709\u66f4\u597d\u7684\u67e5\u627e\u6027\u80fd\uff0c\u800c\u54c8\u5e0c\u8868\u6bd4\u6570\u7ec4\u5177\u6709\u66f4\u5feb\u7684\u67e5\u627e\u548c\u63d2\u5165\u6027\u80fd\u3002<\/li>\n<li><strong>\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff1a<\/strong>\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500\uff0c\u56e0\u6b64\u5e94\u5c3d\u53ef\u80fd\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u3002<\/li>\n<li><strong>\u7f13\u5b58\u7ed3\u679c\uff1a<\/strong>\u5982\u679c\u8ba1\u7b97\u7ed3\u679c\u9700\u8981\u91cd\u590d\u4f7f\u7528\uff0c\u53ef\u4ee5\u5c06\u5176\u7f13\u5b58\u8d77\u6765\u4ee5\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/li>\n<li><strong>\u4f7f\u7528 Composer \u81ea\u52a8\u52a0\u8f7d\u5668\uff1a<\/strong>Composer \u662f PHP \u7684\u4f9d\u8d56\u7ba1\u7406\u5668\uff0c\u5b83\u53ef\u4ee5\u81ea\u52a8\u52a0\u8f7d\u7c7b\u548c\u5e93\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406\u793a\u4f8b<\/strong><\/p>\n<pre>&lt;?php\n\nfunction divide($dividend, $divisor)\n{\n    try {\n        if ($divisor === 0) {\n            throw new Exception('\u9664\u6570\u4e0d\u80fd\u4e3a 0');\n        }\n        return $dividend \/ $divisor;\n    } catch (Exception $e) {\n        return '\u9664\u6cd5\u53d1\u751f\u9519\u8bef\uff1a' . $e-&gt;getMessage();\n    }\n}\n\necho divide(10, 2); \/\/ \u8f93\u51fa\uff1a5\necho divide(10, 0); \/\/ \u8f93\u51fa\uff1a\u9664\u6cd5\u53d1\u751f\u9519\u8bef\uff1a\u9664\u6570\u4e0d\u80fd\u4e3a 0<\/pre>\n<p><strong>\u6027\u80fd\u4f18\u5316\u793a\u4f8b<\/strong><\/p>\n<pre>&lt;?php\n\n$array = range(1, 100000);\n\n\/\/ \u4f7f\u7528\u9ad8\u6548\u7684\u6570\u7ec4\u67e5\u627e\n$startTime = microtime(true);\n$result = in_array(50000, $array);\n$endTime = microtime(true);\n\n\/\/ \u4f7f\u7528\u8f83\u4f4e\u6548\u7684\u5faa\u73af\n$startTime2 = microtime(true);\nfor ($i = 0; $i &lt; 100000; $i++) {\n    if ($array[$i] === 50000) {\n        $result2 = true;\n        break;\n    }\n}\n$endTime2 = microtime(true);\n\necho '\u6570\u7ec4\u67e5\u627e\u8017\u65f6\uff1a', ($endTime - $startTime), ' \u79d2' . PHP_EOL;\necho '\u5faa\u73af\u67e5\u627e\u8017\u65f6\uff1a', ($endTime2 - $startTime2), ' \u79d2' . PHP_EOL;<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u67e5\u627e\u6bd4\u5faa\u73af\u67e5\u627e\u8981\u5feb\u5f97\u591a\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u6027\u80fd\u4f18\u5316,\u5f02\u5e38\u5904\u7406\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316 \u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316\u4e00\u3001\u5f02\u5e38\u5904\u7406\u5f02\u5e38\u5904\u7406\u673a\u5236\u5141\u8bb8\u4f18\u96c5\u5730\u6355\u83b7\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u901a\u8fc7 try&#8230;catch \u5757\u6355\u83b7 Throwable \u7c7b\u7684\u5b50\u7c7b\u5f02\u5e38\u3002\u4e8c\u3001\u6027\u80fd\u4f18\u5316\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u54c8\u5e0c\u8868\uff09\u3002\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u3002\u7f13\u5b58\u7ed3\u679c\u3002\u4f7f\u7528 Composer \u81ea\u52a8\u52a0\u8f7d\u5668\u3002\u5b9e\u6218\u6848\u4f8b\u5f02\u5e38\u5904\u7406\uff1a\u9664\u6570\u4e3a 0 \u65f6\u629b\u51fa\u5f02\u5e38\uff0c\u63d0\u4f9b\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u3002\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528\u6570\u7ec4\u67e5\u627e\u6bd4\u5faa\u73af\u67e5\u627e\u66f4\u5feb\u3002 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6027\u80fd\u4f18\u5316 \u5f02\u5e38\u5904\u7406 \u5f02\u5e38\u5904\u7406\u673a\u5236\u53ef\u4ee5\u8ba9\u5f00\u53d1\u4eba\u5458\u4f18\u96c5\u5730\u6355\u83b7\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u662f Throwable \u7c7b\u7684\u5b50\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528 try&#8230;catch \u5757\u6765\u6355\u83b7\u5b83\u4eec\u3002 &lt;?php try { throw new Exception(&#8216;\u9519\u8bef\u4fe1\u606f&#8217;); } catch (Exception $e) { \/\/ \u5904\u7406\u9519\u8bef } \u901a\u8fc7\u6355\u83b7\u5f02\u5e38\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u63d0\u4f9b\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f\u6216\u91c7\u53d6\u9002\u5f53\u7684\u8865\u6551\u63aa\u65bd\u3002 \u6027\u80fd\u4f18\u5316 PHP \u6027\u80fd\u4f18\u5316\u6d89\u53ca\u5230\u6d88\u9664\u74f6\u9888\u548c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u4f18\u5316\u7b56\u7565\uff1a \u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff1a\u6570\u7ec4\u6bd4\u94fe\u8868\u5177\u6709\u66f4\u597d\u7684\u67e5\u627e\u6027\u80fd\uff0c\u800c\u54c8\u5e0c\u8868\u6bd4\u6570\u7ec4\u5177\u6709\u66f4\u5feb\u7684\u67e5\u627e\u548c\u63d2\u5165\u6027\u80fd\u3002 \u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff1a\u6bcf\u6b21\u51fd\u6570\u8c03\u7528\u90fd\u4f1a\u4ea7\u751f\u5f00\u9500\uff0c\u56e0\u6b64\u5e94\u5c3d\u53ef\u80fd\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u3002 \u7f13\u5b58\u7ed3\u679c\uff1a\u5982\u679c\u8ba1\u7b97\u7ed3\u679c\u9700\u8981\u91cd\u590d\u4f7f\u7528\uff0c\u53ef\u4ee5\u5c06\u5176\u7f13\u5b58\u8d77\u6765\u4ee5\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002 \u4f7f\u7528 Composer \u81ea\u52a8\u52a0\u8f7d\u5668\uff1aComposer \u662f PHP \u7684\u4f9d\u8d56\u7ba1\u7406\u5668\uff0c\u5b83\u53ef\u4ee5\u81ea\u52a8\u52a0\u8f7d\u7c7b\u548c\u5e93\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002 \u5b9e\u6218\u6848\u4f8b \u5f02\u5e38\u5904\u7406\u793a\u4f8b &lt;?php function [&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-47128","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47128","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=47128"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47128\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}