{"id":48246,"date":"2024-12-02T14:57:34","date_gmt":"2024-12-02T06:57:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48246\/"},"modified":"2024-12-02T14:57:34","modified_gmt":"2024-12-02T06:57:34","slug":"php-%e5%87%bd%e6%95%b0%e4%b8%ad%e5%a0%86%e6%a0%88%e6%ba%a2%e5%87%ba%e6%97%b6%e7%9a%84%e8%b0%83%e8%af%95%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48246\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aPHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p>\u5806\u6808\u6ea2\u51fa\u662f\u4e00\u79cd\u56e0\u51fd\u6570\u8c03\u7528\u8fc7\u591a\u800c\u5bfc\u81f4\u5185\u5b58\u8017\u5c3d\u7684\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u8c03\u8bd5\u6280\u5de7\u5305\u62ec\uff1a\u4f7f\u7528 debug_backtrace() \u51fd\u6570\u663e\u793a\u5806\u6808\u8ffd\u8e2a\uff0c\u5b9a\u4f4d\u5806\u6808\u6ea2\u51fa\u51fd\u6570\u3002\u589e\u5927 PHP \u811a\u672c\u7684\u5185\u5b58\u9650\u5236\u3002\u9650\u5236\u51fd\u6570\u5d4c\u5957\u6df1\u5ea6\uff0c\u4f7f\u7528 xdebug.max_nesting_level \u6307\u4ee4\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730004796671dc73c46428.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7<\/h2>\n<p><strong>\u4ec0\u4e48\u662f\u5806\u6808\u6ea2\u51fa\uff1f<\/strong><\/p>\n<p>\u5806\u6808\u6ea2\u51fa\u662f\u4e00\u79cd\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u5b83\u53d1\u751f\u5728\u51fd\u6570\u8c03\u7528\u8fc7\u591a\uff0c\u5bfc\u81f4\u7cfb\u7edf\u4e3a\u51fd\u6570\u5206\u914d\u7684\u5185\u5b58\u7528\u5c3d\u3002<\/p>\n<p><strong>\u8c03\u8bd5\u5806\u6808\u6ea2\u51fa\u7684\u6280\u5de7<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8c03\u8bd5\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u6709\u7528\u6280\u5de7\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 <code>debug_backtrace()<\/code><\/strong><\/p>\n<p><code>debug_backtrace()<\/code> \u51fd\u6570\u663e\u793a\u5f53\u524d\u5806\u6808\u8ffd\u8e2a\uff0c\u5305\u62ec\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u884c\u53f7\u548c\u6587\u4ef6\u540d\u3002\u8fd9\u6709\u52a9\u4e8e\u5b9a\u4f4d\u5806\u6808\u6ea2\u51fa\u53d1\u751f\u7684\u51fd\u6570\u3002<\/p>\n<pre>&lt;?php\nfunction foo() {\n  foo();\n}\nfoo();\n\necho '&lt;pre&gt;';\nprint_r(debug_backtrace());\necho '&lt;\/pre&gt;';\n?&gt;<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>Array\n(\n    [0] =&gt; Array\n        (\n            [file] =&gt; filename.php\n            [line] =&gt; 8\n            [function] =&gt; foo\n            [args] =&gt; Array\n                (\n                )\n        )\n    [1] =&gt; Array\n        (\n            [file] =&gt; filename.php\n            [line] =&gt; 7\n            [function] =&gt; foo\n            [args] =&gt; Array\n                (\n                )\n        )\n    [2] =&gt; Array\n        (\n            [file] =&gt; filename.php\n            [line] =&gt; 13\n            [function] =&gt; {main}\n            [args] =&gt; Array\n                (\n                )\n        )\n)<\/pre>\n<p><strong>2. \u589e\u52a0 PHP \u811a\u672c\u7684\u5185\u5b58\u9650\u5236<\/strong><\/p>\n<p>\u5982\u679c\u5806\u6808\u6ea2\u51fa\u662f\u7531\u5185\u5b58\u4e0d\u8db3\u9020\u6210\u7684\uff0c\u53ef\u4ee5\u589e\u52a0 PHP \u811a\u672c\u7684\u5185\u5b58\u9650\u5236\u3002<\/p>\n<pre>&lt;?php\nini_set('memory_limit', '512M');\n?&gt;<\/pre>\n<p><strong>3. \u4f7f\u7528 <code>xdebug.max_nesting_level<\/code><\/strong><\/p>\n<p><code>xdebug.max_nesting_level<\/code> \u6307\u4ee4\u9650\u5236\u51fd\u6570\u5d4c\u5957\u7684\u5c42\u6570\u3002\u5982\u679c\u5806\u6808\u6ea2\u51fa\u662f\u7531\u51fd\u6570\u5d4c\u5957\u8fc7\u591a\u9020\u6210\u7684\uff0c\u53ef\u4ee5\u589e\u52a0\u6b64\u503c\u3002<\/p>\n<pre>&lt;?php\nini_set('xdebug.max_nesting_level', 256);\n?&gt;<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u793a\u4f8b\u51fd\u6570\uff1a<\/strong><\/p>\n<pre>&lt;?php\nfunction factorial($n) {\n  if ($n == 0) {\n    return 1;\n  }\n  return $n * factorial($n - 1);\n}\n?&gt;<\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u8ba1\u7b97\u4e00\u4e2a\u6570\u5b57\u7684\u9636\u4e58\u3002\u5982\u679c\u4f20\u9012\u4e00\u4e2a\u5f88\u5927\u7684\u6570\u5b57\uff08\u4f8b\u5982 10000\uff09\uff0c\u5b83\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\uff0c\u56e0\u4e3a\u51fd\u6570\u4f1a\u65e0\u9650\u9012\u5f52\u3002<\/p>\n<p><strong>\u542f\u7528\u5806\u6808\u8ffd\u8e2a\uff1a<\/strong><\/p>\n<pre>&lt;?php\nerror_reporting(E_ALL);\nini_set('display_errors', 1);\n?&gt;<\/pre>\n<p>\u8fd9\u5c06\u542f\u7528\u9519\u8bef\u62a5\u544a\u548c\u5806\u6808\u8ffd\u8e2a\u8f93\u51fa\u3002<\/p>\n<p><strong>\u6d4b\u8bd5\u51fd\u6570\uff1a<\/strong><\/p>\n<pre>&lt;?php\nfactorial(10000);\n?&gt;<\/pre>\n<p>\u8fd9\u5c06\u4ea7\u751f\u4ee5\u4e0b\u5806\u6808\u6ea2\u51fa\u9519\u8bef\uff1a<\/p>\n<pre>Fatal error: Maximum function nesting level of '100' reached, aborting!<\/pre>\n<p><strong>\u8c03\u8bd5\u5806\u6808\u6ea2\u51fa\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 <code>debug_backtrace()<\/code> \u5b9a\u4f4d\u51fd\u6570\u8c03\u7528\uff1a<\/p>\n<pre>&lt;?php\nerror_reporting(E_ALL);\nini_set('display_errors', 1);\n\nfunction factorial($n) {\n  if ($n == 0) {\n    return 1;\n  }\n  print_r(debug_backtrace());\n  return $n * factorial($n - 1);\n}\nfactorial(10000);\n?&gt;<\/pre>\n<p>\u8fd9\u5c06\u663e\u793a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u6709\u52a9\u4e8e\u5b9a\u4f4d\u9012\u5f52\u8c03\u7528\u8fc7\u591a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u7684\u51fd\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7 \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aPHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 \u5806\u6808\u6ea2\u51fa\u662f\u4e00\u79cd\u56e0\u51fd\u6570\u8c03\u7528\u8fc7\u591a\u800c\u5bfc\u81f4\u5185\u5b58\u8017\u5c3d\u7684\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u8c03\u8bd5\u6280\u5de7\u5305\u62ec\uff1a\u4f7f\u7528 debug_backtrace() \u51fd\u6570\u663e\u793a\u5806\u6808\u8ffd\u8e2a\uff0c\u5b9a\u4f4d\u5806\u6808\u6ea2\u51fa\u51fd\u6570\u3002\u589e\u5927 PHP \u811a\u672c\u7684\u5185\u5b58\u9650\u5236\u3002\u9650\u5236\u51fd\u6570\u5d4c\u5957\u6df1\u5ea6\uff0c\u4f7f\u7528 xdebug.max_nesting_level \u6307\u4ee4\u3002 PHP \u51fd\u6570\u4e2d\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u8c03\u8bd5\u6280\u5de7 \u4ec0\u4e48\u662f\u5806\u6808\u6ea2\u51fa\uff1f \u5806\u6808\u6ea2\u51fa\u662f\u4e00\u79cd\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u5b83\u53d1\u751f\u5728\u51fd\u6570\u8c03\u7528\u8fc7\u591a\uff0c\u5bfc\u81f4\u7cfb\u7edf\u4e3a\u51fd\u6570\u5206\u914d\u7684\u5185\u5b58\u7528\u5c3d\u3002 \u8c03\u8bd5\u5806\u6808\u6ea2\u51fa\u7684\u6280\u5de7 \u4ee5\u4e0b\u662f\u4e00\u4e9b\u8c03\u8bd5\u5806\u6808\u6ea2\u51fa\u65f6\u7684\u6709\u7528\u6280\u5de7\uff1a 1. \u4f7f\u7528 debug_backtrace() debug_backtrace() \u51fd\u6570\u663e\u793a\u5f53\u524d\u5806\u6808\u8ffd\u8e2a\uff0c\u5305\u62ec\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u7684\u884c\u53f7\u548c\u6587\u4ef6\u540d\u3002\u8fd9\u6709\u52a9\u4e8e\u5b9a\u4f4d\u5806\u6808\u6ea2\u51fa\u53d1\u751f\u7684\u51fd\u6570\u3002 &lt;?php function foo() { foo(); } foo(); echo &#8216;&lt;pre&gt;&#8217;; print_r(debug_backtrace()); echo &#8216;&lt;\/pre&gt;&#8217;; ?&gt; \u8f93\u51fa\uff1a Array ( [0] =&gt; Array ( [file] =&gt; filename.php [line] =&gt; 8 [function] =&gt; foo [args] =&gt; Array ( [&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-48246","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48246","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=48246"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48246\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}