{"id":48330,"date":"2024-12-02T13:47:16","date_gmt":"2024-12-02T05:47:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48330\/"},"modified":"2024-12-02T13:47:16","modified_gmt":"2024-12-02T05:47:16","slug":"php-%e4%b8%ad%e5%a0%86%e6%a0%88%e6%ba%a2%e5%87%ba%e7%9a%84%e6%9b%bf%e4%bb%a3%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48330\/","title":{"rendered":"PHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>PHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u6709\uff1a\u8bbe\u7f6e\u9519\u8bef\u5904\u7406\u51fd\u6570\uff1a\u4f7f\u7528 set_error_handler() \u6ce8\u518c\u81ea\u5b9a\u4e49\u51fd\u6570\u5904\u7406\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u4f7f\u7528 try-catch \u8bed\u53e5\uff1a\u6355\u83b7\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u5e76\u6267\u884c\u7279\u5b9a\u4ee3\u7801\u3002\u4f7f\u7528\u4ee3\u7801\u9650\u5236\u6307\u4ee4\uff1a\u901a\u8fc7 set_time_limit() \u548c set_memory_limit() \u9650\u5236\u811a\u672c\u6267\u884c\u65f6\u95f4\u548c\u5185\u5b58\u4f7f\u7528\uff0c\u9632\u6b62\u5806\u6808\u6ea2\u51fa\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730014475671ded0ba5715.jpg\" class=\"aligncenter\" title=\"PHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u63d2\u56fe\" alt=\"PHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u5806\u6808\u6ea2\u51fa\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u901a\u5e38\u662f\u7531\u9012\u5f52\u51fd\u6570\u6216\u65e0\u9650\u5faa\u73af\u9020\u6210\u7684\u3002\u5f53\u5806\u6808\u6ea2\u51fa\u65f6\uff0c\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u5e76\u5d29\u6e83\u3002<\/p>\n<p><strong>\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u5806\u6808\u6ea2\u51fa\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u66ff\u4ee3\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u6765\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u8bbe\u7f6e\u9519\u8bef\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>set_error_handler()<\/code> \u51fd\u6570\u6765\u8bbe\u7f6e\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002\u5f53\u51fa\u73b0\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u65f6\uff0c\u6b64\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u3002<\/p>\n<pre>function myErrorHandler($errno, $errstr, $errfile, $errline) {\n  echo \"\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u9519\u8bef\uff1a\\n\";\n  echo \"\u9519\u8bef\u4ee3\u7801\uff1a$errno\\n\";\n  echo \"\u9519\u8bef\u4fe1\u606f\uff1a$errstr\\n\";\n  echo \"\u9519\u8bef\u6587\u4ef6\uff1a$errfile\\n\";\n  echo \"\u9519\u8bef\u884c\u53f7\uff1a$errline\\n\";\n}\n\nset_error_handler('myErrorHandler');<\/pre>\n<p><strong>2. \u4f7f\u7528 try-catch \u8bed\u53e5<\/strong><\/p>\n<p><code>try-catch<\/code> \u8bed\u53e5\u53ef\u4ee5\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u5305\u62ec\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002<\/p>\n<pre>try {\n  \/\/ \u53ef\u80fd\u4f1a\u5f15\u53d1\u5806\u6808\u6ea2\u51fa\u7684\u4ee3\u7801\u5757\n} catch (Error $e) {\n  \/\/ \u5f53\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u65f6\u6267\u884c\u7684\u4ee3\u7801\u5757\n}<\/pre>\n<p><strong>3. \u4f7f\u7528\u4ee3\u7801\u9650\u5236\u6307\u4ee4<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>set_time_limit()<\/code> \u548c <code>set_memory_limit()<\/code> \u7b49 PHP \u4ee3\u7801\u9650\u5236\u6307\u4ee4\u6765\u9632\u6b62\u5806\u6808\u6ea2\u51fa\u3002\u8fd9\u4e9b\u6307\u4ee4\u5206\u522b\u7528\u4e8e\u9650\u5236\u811a\u672c\u6267\u884c\u65f6\u95f4\u548c\u5185\u5b58\u4f7f\u7528\u91cf\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e0b\u9762\u7684\u9012\u5f52\u51fd\u6570\uff1a<\/p>\n<pre>function factorial($n) {\n  if ($n == 0) {\n    return 1;\n  } else {\n    return $n * factorial($n - 1);\n  }\n}<\/pre>\n<p>\u5f53 <code>factorial(10000)<\/code> \u65f6\uff0c\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u6765\u907f\u514d\u6b64\u95ee\u9898\u3002<\/p>\n<p><strong>\u4f7f\u7528\u9519\u8bef\u5904\u7406\u51fd\u6570<\/strong><\/p>\n<pre>set_error_handler(function($errno, $errstr, $errfile, $errline) {\n  if ($errno == E_WARNING &amp;&amp; $errstr == \"Argument stack limit exceeded.\") {\n    echo \"\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u9519\u8bef\uff01\";\n  }\n});\n\nfactorial(10000);<\/pre>\n<p><strong>\u4f7f\u7528 try-catch \u8bed\u53e5<\/strong><\/p>\n<pre>try {\n  factorial(10000);\n} catch (Error $e) {\n  echo \"\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u9519\u8bef\uff01\";\n}<\/pre>\n<p>\u4f7f\u7528\u8fd9\u4e9b\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u7684\u60c5\u51b5\u4e0b\u5904\u7406\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\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 \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5 \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01 PHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u6709\uff1a\u8bbe\u7f6e\u9519\u8bef\u5904\u7406\u51fd\u6570\uff1a\u4f7f\u7528 set_error_handler() \u6ce8\u518c\u81ea\u5b9a\u4e49\u51fd\u6570\u5904\u7406\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u4f7f\u7528 try-catch \u8bed\u53e5\uff1a\u6355\u83b7\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u5e76\u6267\u884c\u7279\u5b9a\u4ee3\u7801\u3002\u4f7f\u7528\u4ee3\u7801\u9650\u5236\u6307\u4ee4\uff1a\u901a\u8fc7 set_time_limit() \u548c set_memory_limit() \u9650\u5236\u811a\u672c\u6267\u884c\u65f6\u95f4\u548c\u5185\u5b58\u4f7f\u7528\uff0c\u9632\u6b62\u5806\u6808\u6ea2\u51fa\u3002 PHP \u4e2d\u5806\u6808\u6ea2\u51fa\u7684\u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5 \u524d\u8a00 \u5728 PHP \u4e2d\uff0c\u5806\u6808\u6ea2\u51fa\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u901a\u5e38\u662f\u7531\u9012\u5f52\u51fd\u6570\u6216\u65e0\u9650\u5faa\u73af\u9020\u6210\u7684\u3002\u5f53\u5806\u6808\u6ea2\u51fa\u65f6\uff0c\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u5e76\u5d29\u6e83\u3002 \u66ff\u4ee3\u5f02\u5e38\u5904\u7406\u65b9\u6cd5 \u4e3a\u4e86\u907f\u514d\u5806\u6808\u6ea2\u51fa\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u66ff\u4ee3\u7684\u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u6765\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a 1. \u8bbe\u7f6e\u9519\u8bef\u5904\u7406\u51fd\u6570 \u60a8\u53ef\u4ee5\u4f7f\u7528 set_error_handler() \u51fd\u6570\u6765\u8bbe\u7f6e\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002\u5f53\u51fa\u73b0\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u65f6\uff0c\u6b64\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u3002 function myErrorHandler($errno, $errstr, $errfile, $errline) { echo &#8220;\u53d1\u751f\u5806\u6808\u6ea2\u51fa\u9519\u8bef\uff1a\\n&#8221;; echo &#8220;\u9519\u8bef\u4ee3\u7801\uff1a$errno\\n&#8221;; echo &#8220;\u9519\u8bef\u4fe1\u606f\uff1a$errstr\\n&#8221;; echo &#8220;\u9519\u8bef\u6587\u4ef6\uff1a$errfile\\n&#8221;; echo &#8220;\u9519\u8bef\u884c\u53f7\uff1a$errline\\n&#8221;; } set_error_handler(&#8216;myErrorHandler&#8217;); 2. \u4f7f\u7528 try-catch \u8bed\u53e5 try-catch \u8bed\u53e5\u53ef\u4ee5\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u5305\u62ec\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002 try { \/\/ \u53ef\u80fd\u4f1a\u5f15\u53d1\u5806\u6808\u6ea2\u51fa\u7684\u4ee3\u7801\u5757 [&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-48330","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48330","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=48330"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48330\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}