{"id":202442,"date":"2025-05-13T14:19:56","date_gmt":"2025-05-13T06:19:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202442\/"},"modified":"2025-05-13T14:19:56","modified_gmt":"2025-05-13T06:19:56","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%ef%bc%9a%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202442\/","title":{"rendered":"PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u6027\u80fd\u4f18\u5316"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u6027\u80fd\u4f18\u5316<\/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\u5c31\u6574\u7406\u5206\u4eab\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u6027\u80fd\u4f18\u5316\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\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>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u901a\u8fc7\u4f7f\u7528\u95ed\u5305\u3001lambda \u8868\u8fbe\u5f0f\u7b49 FP \u6784\u4ef6\u6765\u63d0\u5347\u6027\u80fd\uff1a\u4f7f\u7528\u95ed\u5305\u6355\u83b7\u5916\u90e8\u53d8\u91cf\uff0c\u907f\u514d\u4f20\u9012\u91cd\u590d\u53d8\u91cf\uff1b\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u5347\u6548\u7387\uff1b\u907f\u514d\u72b6\u6001\u53ef\u53d8\uff0c\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u548c\u7eaf\u51fd\u6570\uff1b\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u6b21\u6570\uff0c\u901a\u8fc7\u6279\u5904\u7406\u64cd\u4f5c\u4f18\u5316\u6027\u80fd\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u901a\u8fc7\u5c06\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u5206\u89e3\u4e3a\u95ed\u5305\u548c lambda \u8868\u8fbe\u5f0f\uff0c\u663e\u8457\u63d0\u5347\u4e86\u56fe\u50cf\u5904\u7406\u6548\u7387\uff0c\u540c\u65f6\u4fdd\u6301\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241016\/1729084668670fbcfc53f38.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u63d0\u5347\u6027\u80fd<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\uff08FP\uff09\u662f\u7f16\u7a0b\u8303\u5f0f\uff0c\u5f3a\u8c03\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u548c\u7eaf\u51fd\u6570\u3002PHP \u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u901a\u8fc7\u4f7f\u7528\u95ed\u5305\u3001lambda \u8868\u8fbe\u5f0f\u548c\u5176\u4ed6 FP \u6784\u4ef6\u6765\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p><strong>1. \u4f7f\u7528\u95ed\u5305\u6355\u83b7\u5916\u90e8\u53d8\u91cf<\/strong><\/p>\n<p>\u95ed\u5305\u5141\u8bb8\u60a8\u4ece\u5916\u90e8\u4f5c\u7528\u57df\u6355\u83b7\u53d8\u91cf\uff0c\u4ece\u800c\u53ef\u4ee5\u5728\u51fd\u6570\u4e4b\u5916\u8bbf\u95ee\u5b83\u4eec\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u56e0\u4e3a\u60a8\u4e0d\u5fc5\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u90fd\u4f20\u9012\u8fd9\u4e9b\u53d8\u91cf\u3002<\/p>\n<pre>$num = 10;\n\n$closure = function($x) use ($num) {\n    return $x * $num;\n};\n\necho $closure(5); \/\/ 50<\/pre>\n<p><strong>2. \u4f7f\u7528 lambda \u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>lambda \u8868\u8fbe\u5f0f\u662f\u533f\u540d\u51fd\u6570\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<pre>$nums = [1, 2, 3, 4, 5];\n\n$result = array_map(function($x) {\n    return $x * $x;\n}, $nums);\n\nprint_r($result); \/\/ [1, 4, 9, 16, 25]<\/pre>\n<p><strong>3. \u907f\u514d\u72b6\u6001\u53ef\u53d8<\/strong><\/p>\n<p>\u5c3d\u91cf\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u548c\u7eaf\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u51fd\u6570\u7684\u8f93\u51fa\u4ec5\u53d6\u51b3\u4e8e\u5b83\u7684\u8f93\u5165\u3002\u907f\u514d\u66f4\u65b0\u73b0\u6709\u53d8\u91cf\uff0c\u800c\u662f\u8fd4\u56de\u65b0\u503c\u3002<\/p>\n<pre>\/\/ \u53ef\u53d8\n$arr = [1, 2, 3];\n$arr[] = 4;\n\n\/\/ \u4e0d\u53ef\u53d8\n$arr = [1, 2, 3];\n$arr = array_push($arr, 4);<\/pre>\n<p><strong>4. \u51cf\u5c11\u51fd\u6570\u8c03\u7528\u6b21\u6570<\/strong><\/p>\n<p>\u6279\u5904\u7406\u64cd\u4f5c\u53ef\u4ee5\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<pre>\/\/ \u4e00\u6b21\u6027\u8c03\u7528\n$result = array_filter($nums, function($x) {\n    return $x % 2 === 0;\n});\n\n\/\/ \u5206\u6b65\u8c03\u7528\n$temp = array_map(function($x) {\n    return $x % 2 === 0;\n}, $nums);\n\n$result = array_filter($temp);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u4f7f\u7528 FP \u6280\u672f\u9ad8\u6548\u5730\u5904\u7406\u56fe\u50cf\uff1a<\/p>\n<pre>use Intervention\\Image\\Image;\n\n$img = Image::make('image.jpg');\n\n\/\/ \u8c03\u6574\u5927\u5c0f\u548c\u88c1\u526a\n$img-&gt;resize(300, 200)-&gt;crop(150, 100);\n\n\/\/ \u5e94\u7528\u6ee4\u955c\n$img-&gt;filter('grayscale');\n\n\/\/ \u4fdd\u5b58\u56fe\u50cf\n$img-&gt;save('new-image.jpg');<\/pre>\n<p>\u901a\u8fc7\u5c06\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u5206\u89e3\u4e3a\u95ed\u5305\u548c lambda \u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\uff0c\u540c\u65f6\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8ephp,\u51fd\u6570\u5f0f\u7f16\u7a0b\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\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u6027\u80fd\u4f18\u5316\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u6027\u80fd\u4f18\u5316&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202442","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202442"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202442\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}