{"id":48117,"date":"2024-12-02T16:57:10","date_gmt":"2024-12-02T08:57:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48117\/"},"modified":"2024-12-02T16:57:10","modified_gmt":"2024-12-02T08:57:10","slug":"php%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e7%94%9f%e6%88%90%e5%99%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48117\/","title":{"rendered":"PHP\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\uff1f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aPHP\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729990461671d8f3d96140.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\uff1f\u63d2\u56fe\" alt=\"PHP\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570<\/strong><\/p>\n<p><strong>\u4ecb\u7ecd<\/strong><\/p>\n<p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684 PHP \u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u9010\u4e2a\u751f\u6210\u503c\uff0c\u800c\u65e0\u9700\u5b58\u50a8\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u65e0\u9650\u5e8f\u5217\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>\u751f\u6210\u5668\u4f7f\u7528 yield \u5173\u952e\u5b57\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>function fibonacci() {\n  $a = 0;\n  $b = 1;\n  while(true) {\n    yield $a;\n    $c = $a + $b;\n    $a = $b;\n    $b = $c;\n  }\n}<\/pre>\n<p><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 foreach \u904d\u5386\u751f\u6210\u5668\uff1a<\/p>\n<pre>$fib = fibonacci();\nforeach ($fib as $num) {\n  \/\/ \u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\u6765\u5904\u7406\u4e00\u4e2a\u5927\u578b\u6587\u4ef6\uff1a<\/p>\n<pre>function readLargeFile($filename) {\n  $handle = fopen($filename, \"r\");\n  while (($line = fgets($handle)) !== false) {\n    yield $line;\n  }\n  fclose($handle);\n}\n\n$file = readLargeFile(\"large_file.txt\");\nforeach ($file as $line) {\n  \/\/ \u6bcf\u884c\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\n}<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u751f\u6210\u5668\u9010\u884c\u751f\u6210\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u800c\u65e0\u9700\u4e00\u6b21\u6027\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u8282\u7ea6\u4e86\u5185\u5b58\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u751f\u6210\u5668\u65f6\uff0c\u8bf7\u8bb0\u4f4f\u751f\u6210\u7684\u503c\u4e0d\u4f1a\u88ab\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u56e0\u6b64\u5b83\u4eec\u53ea\u80fd\u88ab\u904d\u5386\u4e00\u6b21\u3002<\/li>\n<li>\u751f\u6210\u5668\u4e0d\u80fd\u5305\u542b return \u8bed\u53e5\uff0c\u56e0\u4e3a\u5b83\u4f1a\u7ec8\u6b62\u751f\u6210\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\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\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\uff1f \u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aPHP\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002 \u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570 \u4ecb\u7ecd \u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684 PHP \u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u9010\u4e2a\u751f\u6210\u503c\uff0c\u800c\u65e0\u9700\u5b58\u50a8\u5b83\u4eec\u5728\u5185\u5b58\u4e2d\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u65e0\u9650\u5e8f\u5217\u975e\u5e38\u6709\u7528\u3002 \u8bed\u6cd5 \u751f\u6210\u5668\u4f7f\u7528 yield \u5173\u952e\u5b57\u6765\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4f8b\u5982\uff1a function fibonacci() { $a = 0; $b = 1; while(true) { yield $a; $c = $a + $b; $a = $b; $b = $c; } } \u4f7f\u7528\u751f\u6210\u5668 \u53ef\u4ee5\u4f7f\u7528 foreach \u904d\u5386\u751f\u6210\u5668\uff1a $fib = fibonacci(); foreach ($fib as $num) { \/\/ \u6267\u884c\u4e00\u4e9b\u64cd\u4f5c } \u5b9e\u6218\u6848\u4f8b [&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-48117","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48117","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=48117"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48117\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}