{"id":47024,"date":"2024-12-02T09:39:59","date_gmt":"2024-12-02T01:39:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47024\/"},"modified":"2024-12-02T09:39:59","modified_gmt":"2024-12-02T01:39:59","slug":"%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e6%88%96%e7%94%9f%e6%88%90%e5%99%a8%e4%bc%98%e5%8c%96-php-%e5%87%bd%e6%95%b0%e4%bb%a5%e9%81%bf%e5%85%8d%e5%a0%86%e6%a0%88%e6%ba%a2%e5%87%ba","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47024\/","title":{"rendered":"\u4f7f\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa<\/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\u300a\u4f7f\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa\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>PHP \u4e2d\u907f\u514d\u5806\u6808\u6ea2\u51fa\uff1a\u534f\u7a0b\uff1a\u5c06\u5d4c\u5957\u51fd\u6570\u5206\u914d\u5230\u4e0d\u540c\u7684\u534f\u7a0b\u4e2d\uff0c\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002\u751f\u6210\u5668\uff1a\u4f7f\u7528\u6309\u9700\u751f\u6210\u503c\u5e8f\u5217\uff0c\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729860636671b941c5bb98.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa\u63d2\u56fe\" alt=\"\u4f7f\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa\u63d2\u56fe\" \/><\/p>\n<p><strong>\u907f\u514d PHP \u51fd\u6570\u5806\u6808\u6ea2\u51fa\uff1a\u5de7\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5927\u578b\u5d4c\u5957\u51fd\u6570\u6216\u9012\u5f52\u51fd\u6570\u5728 PHP \u4e2d\u5f88\u5bb9\u6613\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u534f\u7a0b\u548c\u751f\u6210\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u4f18\u5316\u8fd9\u4e9b\u51fd\u6570\uff0c\u540c\u65f6\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002<\/p>\n<p><strong>\u534f\u7a0b<\/strong><\/p>\n<p>\u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5b83\u5141\u8bb8\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u540c\u65f6\u6267\u884c\u591a\u4e2a\u51fd\u6570\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5c06\u5d4c\u5957\u51fd\u6570\u5206\u914d\u5230\u4e0d\u540c\u7684\u534f\u7a0b\uff0c\u4ece\u800c\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002<\/p>\n<pre>use Generator;\n\nfunction factorial(int $n): Generator\n{\n    if ($n &lt;= 1) {\n        yield 1;\n    } else {\n        for ($i = $n; yield from factorial($i - 1); $i--) {}\n    }\n}\n\n$result = 1;\nforeach (factorial(10000) as $value) {\n    $result *= $value;\n}\n\necho $result; \/\/ \u8ba1\u7b97\u7ed3\u679c<\/pre>\n<p><strong>\u751f\u6210\u5668<\/strong><\/p>\n<p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7c7b\u4f3c\u534f\u7a0b\u7684\u7ed3\u6784\u3002\u5b83\u5141\u8bb8\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5176\u4e2d\u5305\u542b\u6309\u9700\u751f\u6210\u7684\u503c\u5e8f\u5217\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u4ee5\u5206\u5757\u7684\u65b9\u5f0f\u5904\u7406\u6570\u636e\uff0c\u4ece\u800c\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002<\/p>\n<pre>function fibonacci(int $n): Generator\n{\n    $a = 0;\n    $b = 1;\n    while ($n--) {\n        yield $a;\n        $tmp = $b;\n        $b += $a;\n        $a = $tmp;\n    }\n}\n\n$fibs = [];\nforeach (fibonacci(100000) as $value) {\n    $fibs[] = $value;\n}\n\necho implode(', ', $fibs); \/\/ \u62fc\u63a5\u6590\u6ce2\u90a3\u5951\u6570\u5217<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u8ba1\u7b97\u5927\u6570\u5b57\u9636\u4e58<\/strong><\/p>\n<pre>function factorial(int $n): int\n{\n    $result = 1;\n    for ($i = 2; $i &lt;= $n; $i++) {\n        $result *= $i;\n    }\n    return $result;\n}<\/pre>\n<p><strong>\u6539\u8fdb\uff1a\u4f7f\u7528\u751f\u6210\u5668<\/strong><\/p>\n<pre>function factorial(int $n): Generator\n{\n    if ($n &lt;= 1) {\n        yield 1;\n    } else {\n        for ($i = $n; yield from factorial($i - 1); $i--) {}\n    }\n}\n\n$result = 1;\nforeach (factorial(10000) as $value) {\n    $result *= $value;\n}\n\necho $result;<\/pre>\n<p><strong>\u7f3a\u70b9<\/strong><\/p>\n<ul>\n<li>\u534f\u7a0b\u548c\u751f\u6210\u5668\u5728 PHP 5.5 \u53ca\u4e4b\u540e\u7248\u672c\u4e2d\u53ef\u7528\u3002<\/li>\n<li>\u534f\u7a0b\u9700\u8981\u7279\u6b8a\u7684\u6269\u5c55\uff08\u4f8b\u5982 Amp \u6216 ReactPHP\uff09\u6765\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4f7f\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa \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\u300a\u4f7f\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u907f\u514d\u5806\u6808\u6ea2\u51fa\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 PHP \u4e2d\u907f\u514d\u5806\u6808\u6ea2\u51fa\uff1a\u534f\u7a0b\uff1a\u5c06\u5d4c\u5957\u51fd\u6570\u5206\u914d\u5230\u4e0d\u540c\u7684\u534f\u7a0b\u4e2d\uff0c\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002\u751f\u6210\u5668\uff1a\u4f7f\u7528\u6309\u9700\u751f\u6210\u503c\u5e8f\u5217\uff0c\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002 \u907f\u514d PHP \u51fd\u6570\u5806\u6808\u6ea2\u51fa\uff1a\u5de7\u7528\u534f\u7a0b\u6216\u751f\u6210\u5668 \u7b80\u4ecb \u5927\u578b\u5d4c\u5957\u51fd\u6570\u6216\u9012\u5f52\u51fd\u6570\u5728 PHP \u4e2d\u5f88\u5bb9\u6613\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u534f\u7a0b\u548c\u751f\u6210\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u4f18\u5316\u8fd9\u4e9b\u51fd\u6570\uff0c\u540c\u65f6\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002 \u534f\u7a0b \u534f\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7ebf\u7a0b\uff0c\u5b83\u5141\u8bb8\u5728\u5355\u4e2a\u8fdb\u7a0b\u4e2d\u540c\u65f6\u6267\u884c\u591a\u4e2a\u51fd\u6570\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5c06\u5d4c\u5957\u51fd\u6570\u5206\u914d\u5230\u4e0d\u540c\u7684\u534f\u7a0b\uff0c\u4ece\u800c\u907f\u514d\u5806\u6808\u6ea2\u51fa\u3002 use Generator; function factorial(int $n): Generator { if ($n &lt;= 1) { yield 1; } else { for ($i = $n; yield from factorial($i &#8211; 1); $i&#8211;) {} } } $result = 1; foreach (factorial(10000) as $value) { [&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-47024","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47024","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=47024"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47024\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}