{"id":48518,"date":"2024-12-02T12:03:16","date_gmt":"2024-12-02T04:03:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48518\/"},"modified":"2024-12-02T12:03:16","modified_gmt":"2024-12-02T04:03:16","slug":"%e5%a6%82%e4%bd%95%e9%92%88%e5%af%b9%e4%b8%8d%e5%90%8c%e7%9a%84-php-%e7%89%88%e6%9c%ac%e4%bc%98%e5%8c%96%e9%80%92%e5%bd%92%e5%87%bd%e6%95%b0%e7%9a%84%e5%a0%86%e6%a0%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48518\/","title":{"rendered":"\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730041237671e5595ea9bd.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528\u63d2\u56fe\" alt=\"\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528<\/strong><\/p>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u9012\u5f52\u51fd\u6570\u5728 PHP \u4e2d\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5982\u679c\u5904\u7406\u4e0d\u5f53\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u4e0d\u540c\u7248\u672c\u7684 PHP \u5728\u5904\u7406\u5806\u6808\u65b9\u9762\u5b58\u5728\u5dee\u5f02\uff0c\u56e0\u6b64\u4f18\u5316\u9012\u5f52\u51fd\u6570\u4ee5\u9002\u5e94\u8fd9\u4e9b\u5dee\u5f02\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p><strong>PHP 5.6 \u53ca\u66f4\u4f4e\u7248\u672c<\/strong><\/p>\n<p>\u5728 PHP 5.6 \u53ca\u66f4\u4f4e\u7248\u672c\u4e2d\uff0c\u9012\u5f52\u8c03\u7528\u4f1a\u521b\u5efa\u65b0\u7684\u5806\u6808\u5e27\uff0c\u4ece\u800c\u589e\u52a0\u5806\u6808\u4f7f\u7528\u91cf\u3002\u4e3a\u4e86\u4f18\u5316\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6280\u672f\uff1a<\/p>\n<pre>\/\/ \u81ea\u5b9a\u4e49\u5806\u6808\u7ba1\u7406\u51fd\u6570\nfunction stack_deep($stack = [], $limit = 5000) {\n    if (count($stack) &gt;= $limit) {\n        throw new RuntimeException('Recursion limit exceeded');\n    }\n    $stack[] = debug_backtrace();\n    return $stack;\n}\n\n\/\/ \u5728\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u5806\u6808\u7ba1\u7406\nfunction factorial($n, $stack = []) {\n    $stack = stack_deep($stack);\n    if ($n &lt;= 1) {\n        return 1;\n    }\n    return $n * factorial($n - 1, $stack);\n}<\/pre>\n<p><strong>PHP 7.0 \u53ca\u66f4\u9ad8\u7248\u672c<\/strong><\/p>\n<p>\u4ece PHP 7.0 \u5f00\u59cb\uff0c\u5f15\u5165\u4e86\u4f18\u5316\u9012\u5f52\u8c03\u7528\u7684\u5c3e\u9012\u5f52\u4f18\u5316 (TCO)\u3002TCO \u5141\u8bb8\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5c06\u9012\u5f52\u8c03\u7528\u8f6c\u6362\u4e3a\u8fed\u4ee3\uff0c\u4ece\u800c\u51cf\u5c11\u5806\u6808\u4f7f\u7528\u3002\u4e3a\u4e86\u5229\u7528 TCO\uff0c\u9700\u8981\u786e\u4fdd\u9012\u5f52\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u8c03\u7528\u662f\u9012\u5f52\u8c03\u7528\u3002<\/p>\n<pre>\/\/ \u5229\u7528 TCO \u4f18\u5316\u5c3e\u9012\u5f52\nfunction factorialTCO($n, $mul = 1) {\n    if ($n &lt;= 1) {\n        return $mul;\n    }\n    return factorialTCO($n - 1, $mul * $n);\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u8ba1\u7b97\u9636\u4e58\u7684\u793a\u4f8b\u3002\u9636\u4e58\u51fd\u6570\u4f7f\u7528\u9012\u5f52\u6765\u8ba1\u7b97\u7ed9\u5b9a\u6570\u5b57\u7684\u9636\u4e58\u3002<\/p>\n<pre>\/\/ \u8ba1\u7b97\u9636\u4e58\u7684\u672a\u4f18\u5316\u9012\u5f52\u51fd\u6570\nfunction factorialNaive($n) {\n    if ($n &lt;= 1) {\n        return 1;\n    }\n    return $n * factorialNaive($n - 1);\n}<\/pre>\n<p>\u8fd9\u4e2a\u672a\u4f18\u5316\u7684\u7248\u672c\u5728\u5904\u7406\u5927\u578b\u6570\u5b57\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002<\/p>\n<p><strong>\u4f18\u5316\u6848\u4f8b<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u9488\u5bf9\u4e0d\u540c\u7248\u672c\u7684 PHP \u4f18\u5316\u6b64\u51fd\u6570\u3002\u5bf9\u4e8e PHP 5.6 \u53ca\u66f4\u4f4e\u7248\u672c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u5806\u6808\u7ba1\u7406\uff1a<\/p>\n<pre>\/\/ \u81ea\u5b9a\u4e49\u5806\u6808\u7ba1\u7406\u4f18\u5316\nfunction factorialOptimized56($n) {\n    $stack = [];\n    return factorial($n, $stack);\n}<\/pre>\n<p>\u800c\u5bf9\u4e8e PHP 7.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528 TCO\uff1a<\/p>\n<pre>\/\/ TCO \u4f18\u5316\nfunction factorialOptimized70($n) {\n    return factorialTCO($n);\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u9488\u5bf9\u4e0d\u540c\u7248\u672c\u7684 PHP \u4f18\u5316\u9012\u5f52\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u5e76\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u5bf9\u4e8e PHP 7.0 \u53ca\u66f4\u9ad8\u7248\u672c\uff0c\u5229\u7528 TCO \u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5806\u6808\u4f7f\u7528\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8ephp,\u9012\u5f52\u51fd\u6570\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528 \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u5982\u4f55\u9488\u5bf9\u4e0d\u540c\u7684 PHP \u7248\u672c\u4f18\u5316\u9012\u5f52\u51fd\u6570\u7684\u5806\u6808\u4f7f\u7528 \u524d\u8a00 \u9012\u5f52\u51fd\u6570\u5728 PHP \u4e2d\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5982\u679c\u5904\u7406\u4e0d\u5f53\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u4e0d\u540c\u7248\u672c\u7684 PHP \u5728\u5904\u7406\u5806\u6808\u65b9\u9762\u5b58\u5728\u5dee\u5f02\uff0c\u56e0\u6b64\u4f18\u5316\u9012\u5f52\u51fd\u6570\u4ee5\u9002\u5e94\u8fd9\u4e9b\u5dee\u5f02\u975e\u5e38\u91cd\u8981\u3002 PHP 5.6 \u53ca\u66f4\u4f4e\u7248\u672c \u5728 PHP 5.6 \u53ca\u66f4\u4f4e\u7248\u672c\u4e2d\uff0c\u9012\u5f52\u8c03\u7528\u4f1a\u521b\u5efa\u65b0\u7684\u5806\u6808\u5e27\uff0c\u4ece\u800c\u589e\u52a0\u5806\u6808\u4f7f\u7528\u91cf\u3002\u4e3a\u4e86\u4f18\u5316\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6280\u672f\uff1a \/\/ \u81ea\u5b9a\u4e49\u5806\u6808\u7ba1\u7406\u51fd\u6570 function stack_deep($stack = [], $limit = 5000) { if (count($stack) &gt;= $limit) { throw new RuntimeException(&#8216;Recursion limit exceeded&#8217;); } $stack[] = debug_backtrace(); return $stack; } \/\/ \u5728\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u5806\u6808\u7ba1\u7406 function factorial($n, $stack = [&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-48518","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48518","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=48518"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48518\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}