{"id":47106,"date":"2024-12-02T14:43:39","date_gmt":"2024-12-02T06:43:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47106\/"},"modified":"2024-12-02T14:43:39","modified_gmt":"2024-12-02T06:43:39","slug":"php-%e9%97%ad%e5%8c%85%e4%b8%ad%e7%9a%84%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47106\/","title":{"rendered":"php \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406<\/h1>\n<p>\u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p>\u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u9700\u8c28\u614e\uff0c\u907f\u514d\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u3002\u82e5\u5fc5\u987b\u6355\u83b7\uff0c\u5e94\u6355\u83b7\u503c\u800c\u975e\u5f15\u7528\uff1b\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729866337671baa6146c32.jpg\" class=\"aligncenter\" title=\"php \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u63d2\u56fe\" alt=\"php \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406<\/strong><\/p>\n<p><strong>\u4ecb\u7ecd<\/strong><\/p>\n<p>\u95ed\u5305\u662f\u6307\u5728 PHP \u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u4ee3\u7801\u4e0e\u521b\u5efa\u5b83\u4eec\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u76f8\u5173\u8054\u7684\u65b9\u6cd5\u3002\u7136\u800c\uff0c\u5f53\u95ed\u5305\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u65f6\uff0c\u5c31\u5f15\u5165\u4e86\u5185\u5b58\u7ba1\u7406\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u503c\u6355\u83b7<\/strong><\/p>\n<p>\u5f53\u95ed\u5305\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u65f6\uff0c\u5b83\u4f1a\u6355\u83b7\u8be5\u53d8\u91cf\u7684\u503c\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u5728\u521b\u5efa\u95ed\u5305\u7684\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u95ed\u5305\u4ecd\u53ef\u4ee5\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002<\/p>\n<pre>function createClosure() {\n  $x = 10;\n  \/\/ \u521b\u5efa\u4e00\u4e2a\u95ed\u5305\u5e76\u6355\u83b7 $x \u7684\u503c\n  $closure = function() use ($x) {\n    echo $x;\n  };\n  return $closure;\n}<\/pre>\n<p><strong>\u5f15\u7528\u6355\u83b7<\/strong><\/p>\n<p>\u9664\u4e86\u503c\u6355\u83b7\u4e4b\u5916\uff0c\u95ed\u5305\u8fd8\u53ef\u4ee5\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u95ed\u5305\u5728\u6267\u884c\u671f\u95f4\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u3002<\/p>\n<pre>function createClosure() {\n  $x = 10;\n  \/\/ \u521b\u5efa\u4e00\u4e2a\u95ed\u5305\u5e76\u5bf9 $x \u8fdb\u884c\u5f15\u7528\u6355\u83b7\n  $closure = function() use (&amp;$x) {\n    $x++;\n  };\n  return $closure;\n}<\/pre>\n<p><strong>\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n<p>\u5f53\u95ed\u5305\u6355\u83b7\u5bf9\u5916\u90e8\u53d8\u91cf\u7684\u5f15\u7528\u65f6\uff0c\u5c31\u6709\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u8fd9\u662f\u56e0\u4e3a\u5f53\u95ed\u5305\u88ab\u590d\u5236\u5230\u5176\u4ed6\u5730\u65b9\u6216\u5f15\u7528\u5b83\u65f6\uff0c\u5b83\u4f1a\u4fdd\u7559\u5bf9\u5916\u90e8\u53d8\u91cf\u7684\u5f15\u7528\uff0c\u5373\u4f7f\u5b83\u4e0d\u518d\u4f7f\u7528\u8be5\u53d8\u91cf\u3002<\/p>\n<p><strong>\u89e3\u51b3\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n<p>\u907f\u514d\u95ed\u5305\u4e2d\u7684\u5185\u5b58\u6cc4\u6f0f\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u907f\u514d\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u3002\u5982\u679c\u9700\u8981\u6355\u83b7\u53d8\u91cf\uff0c\u6700\u597d\u6355\u83b7\u8be5\u53d8\u91cf\u7684\u503c\u800c\u4e0d\u662f\u5bf9\u5b83\u7684\u5f15\u7528\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u4e0b\u4e00\u4e2a\u8d28\u6570\u7684\u95ed\u5305\uff1a<\/p>\n<pre>function primeGenerator() {\n  $prime = 2;\n  return function() use (&amp;$prime) {\n    while (true) {\n      \/\/ \u5bfb\u627e\u4e0b\u4e00\u4e2a\u8d28\u6570\n      while (!isPrime($prime)) {\n        $prime++;\n      }\n      yield $prime; \/\/ \u8fd4\u56de\u4e0b\u4e00\u4e2a\u8d28\u6570\n      $prime++;\n    }\n  };\n}<\/pre>\n<p>\u7531\u4e8e\u5916\u90e8\u53d8\u91cf <code>$prime<\/code> \u88ab\u5f15\u7528\uff0c\u8be5\u95ed\u5305\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u5e94\u8be5\u53ea\u6355\u83b7 <code>$prime<\/code> \u7684\u503c\uff1a<\/p>\n<pre>function primeGenerator() {\n  $prime = 2;\n  return function() use ($prime) {\n    while (true) {\n      while (!isPrime($prime)) {\n        $prime++;\n      }\n      yield $prime;\n      $prime++;\n    }\n  };\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u95ed\u5305\u5728\u751f\u6210\u8d28\u6570\u65f6\u4e0d\u518d\u5f15\u7528\u5916\u90e8\u53d8\u91cf\uff0c\u6d88\u9664\u4e86\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406 \u7c73\u4e91\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u300aphp \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01 \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\u9700\u8c28\u614e\uff0c\u907f\u514d\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u3002\u82e5\u5fc5\u987b\u6355\u83b7\uff0c\u5e94\u6355\u83b7\u503c\u800c\u975e\u5f15\u7528\uff1b\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002 PHP \u95ed\u5305\u4e2d\u7684\u5185\u5b58\u7ba1\u7406 \u4ecb\u7ecd \u95ed\u5305\u662f\u6307\u5728 PHP \u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u5c06\u4ee3\u7801\u4e0e\u521b\u5efa\u5b83\u4eec\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u76f8\u5173\u8054\u7684\u65b9\u6cd5\u3002\u7136\u800c\uff0c\u5f53\u95ed\u5305\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u65f6\uff0c\u5c31\u5f15\u5165\u4e86\u5185\u5b58\u7ba1\u7406\u7684\u95ee\u9898\u3002 \u503c\u6355\u83b7 \u5f53\u95ed\u5305\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u65f6\uff0c\u5b83\u4f1a\u6355\u83b7\u8be5\u53d8\u91cf\u7684\u503c\u3002\u8fd9\u610f\u5473\u7740\u5373\u4f7f\u5728\u521b\u5efa\u95ed\u5305\u7684\u51fd\u6570\u8fd4\u56de\u540e\uff0c\u95ed\u5305\u4ecd\u53ef\u4ee5\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002 function createClosure() { $x = 10; \/\/ \u521b\u5efa\u4e00\u4e2a\u95ed\u5305\u5e76\u6355\u83b7 $x \u7684\u503c $closure = function() use ($x) { echo $x; }; return $closure; } \u5f15\u7528\u6355\u83b7 \u9664\u4e86\u503c\u6355\u83b7\u4e4b\u5916\uff0c\u95ed\u5305\u8fd8\u53ef\u4ee5\u5f15\u7528\u5916\u90e8\u53d8\u91cf\u3002\u8fd9\u610f\u5473\u7740\u95ed\u5305\u5728\u6267\u884c\u671f\u95f4\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u3002 function createClosure() { $x = 10; \/\/ \u521b\u5efa\u4e00\u4e2a\u95ed\u5305\u5e76\u5bf9 $x \u8fdb\u884c\u5f15\u7528\u6355\u83b7 $closure = function() use (&amp;$x) { $x++; }; [&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-47106","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47106","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=47106"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47106\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}