{"id":47513,"date":"2024-12-02T17:59:33","date_gmt":"2024-12-02T09:59:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47513\/"},"modified":"2024-12-02T17:59:33","modified_gmt":"2024-12-02T09:59:33","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e5%92%8c%e5%a4%84%e7%90%86-php-%e5%87%bd%e6%95%b0%e4%b8%ad%e7%9a%84%e5%a0%86%e6%a0%88%e6%ba%a2%e5%87%ba%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47513\/","title":{"rendered":"\u5982\u4f55\u68c0\u6d4b\u548c\u5904\u7406 PHP \u51fd\u6570\u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u68c0\u6d4b\u548c\u5904\u7406 PHP \u51fd\u6570\u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u68c0\u6d4b\u548c\u5904\u7406 PHP \u51fd\u6570\u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729921896671c836830e25.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u68c0\u6d4b\u548c\u5904\u7406 PHP \u51fd\u6570\u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u68c0\u6d4b\u548c\u5904\u7406 PHP \u51fd\u6570\u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 PHP \u51fd\u6570\u4e2d\u68c0\u6d4b\u548c\u5904\u7406\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f<\/strong><\/p>\n<p><strong>\u4ecb\u7ecd<\/strong><\/p>\n<p>\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u662f PHP \u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u5b83\u53d1\u751f\u5728\u51fd\u6570\u8c03\u7528\u7684\u5d4c\u5957\u5c42\u7ea7\u8d85\u51fa PHP \u53ef\u7528\u5185\u5b58\uff08\u5806\u6808\uff09\u65f6\u3002\u5b83\u901a\u5e38\u4f1a\u5bfc\u81f4 &#8220;Fatal error: Maximum function nesting level of &#8216;X&#8217; reached&#8221; \u9519\u8bef\u6d88\u606f\u3002<\/p>\n<p><strong>\u68c0\u6d4b<\/strong><\/p>\n<p>\u68c0\u6d4b PHP \u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>try...catch<\/code> \u5757\u6765\u6355\u83b7 <code>StackOverflowError<\/code> \u5f02\u5e38\uff1a<\/p>\n<pre>try {\n    \/\/ \u5177\u6709\u9012\u5f52\u6216\u5d4c\u5957\u8c03\u7528\u7684\u4ee3\u7801\n} catch (StackOverflowError $e) {\n    \/\/ \u5904\u7406\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\n}<\/pre>\n<p><strong>\u5904\u7406<\/strong><\/p>\n<p>\u5904\u7406\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u7684\u6700\u4f73\u65b9\u6cd5\u4e4b\u4e00\u662f\u5728\u51fd\u6570\u4e2d\u51cf\u5c11\u9012\u5f52\u6216\u5d4c\u5957\u8c03\u7528\u7684\u7b49\u7ea7\u3002\u60a8\u53ef\u4ee5\u5c06\u5176\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff0c\u6216\u8003\u8651\u4f7f\u7528\u975e\u9012\u5f52\u7b97\u6cd5\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u8c03\u6574 PHP \u7684\u5806\u6808\u5927\u5c0f\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728 php.ini \u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e <code>xdebug.max_nesting_level<\/code> \u6307\u4ee4\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre>xdebug.max_nesting_level = 256  \/\/ \u5c06\u6700\u5927\u5d4c\u5957\u5c42\u7ea7\u589e\u52a0\u5230 256<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u793a\u4f8b\uff1a<\/p>\n<pre>function factorial($n) {\n    if ($n === 1) {\n        return 1;\n    } else {\n        return $n * factorial($n - 1);  \/\/ \u9012\u5f52\u8c03\u7528\n    }\n}<\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u8ba1\u7b97\u4e00\u4e2a\u6570\u5b57\u7684\u9636\u4e58\u3002\u5982\u679c\u6211\u4eec\u4f20\u5165\u4e00\u4e2a\u8f83\u5927\u7684\u6570\u5b57\uff0c\u4f8b\u5982 <code>1000000<\/code>\uff0c\u5b83\u5c06\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u3002<\/p>\n<p>\u4e3a\u4e86\u4fee\u590d\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u51fd\u6570\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff1a<\/p>\n<pre>function factorial($n) {\n    if ($n === 1) {\n        return 1;\n    }\n\n    $result = $n;\n    for ($i = $n - 1; $i &gt;= 1; $i--) {\n        $result *= $i;\n    }\n\n    return $result;\n}<\/pre>\n<p>\u8fd9\u4e2a\u65b0\u7684\u51fd\u6570\u4f7f\u7528\u5faa\u73af\u800c\u4e0d\u662f\u9012\u5f52\uff0c\u4ece\u800c\u6d88\u9664\u4e86\u5806\u6808\u6ea2\u51fa\u95ee\u9898\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<p>\u5728\u5904\u7406\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u65f6\uff0c\u8bf7\u52a1\u5fc5\u5c0f\u5fc3\u3002\u589e\u52a0 PHP \u7684\u5806\u6808\u5927\u5c0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6d88\u8017\u589e\u52a0\u3002\u6b64\u5916\uff0c\u9012\u5f52\u7b97\u6cd5\u901a\u5e38\u4e0d\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u6df1\u5ea6\u5d4c\u5957\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u68c0\u6d4b\u548c\u5904\u7406 PHP \u51fd\u6570\u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ePHP\u51fd\u6570,\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u68c0\u6d4b\u548c\u5904\u7406 PHP \u51fd\u6570\u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f \u4f60\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\u300a\u5982\u4f55\u68c0\u6d4b\u548c\u5904\u7406 PHP \u51fd\u6570\u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01 \u5982\u4f55\u5728 PHP \u51fd\u6570\u4e2d\u68c0\u6d4b\u548c\u5904\u7406\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\uff1f \u4ecb\u7ecd \u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u662f PHP \u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u8bef\uff0c\u5b83\u53d1\u751f\u5728\u51fd\u6570\u8c03\u7528\u7684\u5d4c\u5957\u5c42\u7ea7\u8d85\u51fa PHP \u53ef\u7528\u5185\u5b58\uff08\u5806\u6808\uff09\u65f6\u3002\u5b83\u901a\u5e38\u4f1a\u5bfc\u81f4 &#8220;Fatal error: Maximum function nesting level of &#8216;X&#8217; reached&#8221; \u9519\u8bef\u6d88\u606f\u3002 \u68c0\u6d4b \u68c0\u6d4b PHP \u4e2d\u7684\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 try&#8230;catch \u5757\u6765\u6355\u83b7 StackOverflowError \u5f02\u5e38\uff1a try { \/\/ \u5177\u6709\u9012\u5f52\u6216\u5d4c\u5957\u8c03\u7528\u7684\u4ee3\u7801 } catch (StackOverflowError $e) { \/\/ \u5904\u7406\u5806\u6808\u6ea2\u51fa\u5f02\u5e38 } \u5904\u7406 \u5904\u7406\u5806\u6808\u6ea2\u51fa\u5f02\u5e38\u7684\u6700\u4f73\u65b9\u6cd5\u4e4b\u4e00\u662f\u5728\u51fd\u6570\u4e2d\u51cf\u5c11\u9012\u5f52\u6216\u5d4c\u5957\u8c03\u7528\u7684\u7b49\u7ea7\u3002\u60a8\u53ef\u4ee5\u5c06\u5176\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u90e8\u5206\uff0c\u6216\u8003\u8651\u4f7f\u7528\u975e\u9012\u5f52\u7b97\u6cd5\u3002 \u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u8c03\u6574 PHP \u7684\u5806\u6808\u5927\u5c0f\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5728 php.ini \u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e xdebug.max_nesting_level \u6307\u4ee4\u6765\u5b9e\u73b0\uff1a xdebug.max_nesting_level [&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-47513","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47513","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=47513"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47513\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}