{"id":202429,"date":"2025-05-13T14:11:53","date_gmt":"2025-05-13T06:11:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202429\/"},"modified":"2025-05-13T14:11:53","modified_gmt":"2025-05-13T06:11:53","slug":"php%e5%87%bd%e6%95%b0%e5%af%b9%e8%b1%a1%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%e4%b8%ad%e7%9a%84%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202429\/","title":{"rendered":"php\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u4e2d\u7684\u5e38\u89c1\u9519\u8bef\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u4e2d\u7684\u5e38\u89c1\u9519\u8bef\u662f\u4ec0\u4e48\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u4e2d\u7684\u5e38\u89c1\u9519\u8bef\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>\u5728 PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\uff1a\u672a\u6b63\u786e\u58f0\u660e\u95ed\u5305\u3001\u4f9d\u8d56\u5168\u5c40\u53d8\u91cf\u3001\u4fee\u6539\u53ef\u53d8\u53d8\u91cf\u3001\u4f7f\u7528\u4e0d\u53ef\u9884\u6d4b\u7684\u526f\u4f5c\u7528\u548c\u6ee5\u7528\u9012\u5f52\u3002\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u901a\u8fc7\u6b63\u786e\u58f0\u660e\u95ed\u5305\uff0c\u4f7f\u7528\u53c2\u6570\u4f20\u9012\u53d8\u91cf\uff0c\u8fd4\u56de\u65b0\u503c\uff0c\u907f\u514d\u526f\u4f5c\u7528\u548c\u8c28\u614e\u4f7f\u7528\u9012\u5f52\u7b49\u65b9\u6cd5\uff0c\u4ece\u800c\u7f16\u5199\u51fa\u51c6\u786e\u4e14\u9ad8\u6548\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u4ee3\u7801\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241015\/1728976432670e16300f620.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u4e2d\u7684\u5e38\u89c1\u9519\u8bef<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b (FP) \u662f\u4e00\u79cd\u8f6f\u4ef6\u5f00\u53d1\u8303\u4f8b\uff0c\u5b83\u5f3a\u8c03\u4f7f\u7528\u7eaf\u51fd\u6570\u3001\u4e0d\u53ef\u53d8\u6570\u636e\u548c\u9012\u5f52\u3002\u5728 PHP \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\u5b9e\u73b0 FP\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528 PHP \u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u65f6\uff0c\u6709\u51e0\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\u3002<\/p>\n<p><strong>1. \u672a\u6b63\u786e\u58f0\u660e\u95ed\u5305<\/strong><\/p>\n<p>\u58f0\u660e\u95ed\u5305\u65f6\uff0c\u8bf7\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u8bed\u6cd5\u3002\u4ee5\u4e0b\u793a\u4f8b\u672a\u6b63\u786e\u58f0\u660e\u95ed\u5305\uff1a<\/p>\n<pre>$func = function ($a, $b) {\n    return $a + $b;\n};<\/pre>\n<p>\u6b63\u786e\u7684\u65b9\u6cd5\u662f\uff1a<\/p>\n<pre>$func = function ($a, $b) {\n    return $a + $b;\n};<\/pre>\n<p><strong>2. \u4f9d\u8d56\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<p>\u95ed\u5305\u53ef\u4ee5\u8bbf\u95ee\u5176\u5b9a\u4e49\u8303\u56f4\u5185\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u5168\u5c40\u53d8\u91cf\u3002\u4f46\u662f\uff0c\u6700\u597d\u907f\u514d\u4f9d\u8d56\u5168\u5c40\u53d8\u91cf\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u4f7f\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u548c\u8c03\u8bd5\u3002\u4ee5\u4e0b\u793a\u4f8b\u4f9d\u8d56\u4e8e\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<pre>$globalVar = 42;\n\n$func = function () {\n    return $globalVar++;\n};<\/pre>\n<p>\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u5c06\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u95ed\u5305\uff1a<\/p>\n<pre>$func = function ($var) {\n    return $var++;\n};\n\n$result = $func($globalVar);<\/pre>\n<p><strong>3. \u4fee\u6539\u53ef\u53d8\u53d8\u91cf<\/strong><\/p>\n<p>\u95ed\u5305\u53ef\u4ee5\u4fee\u6539\u53ef\u53d8\u53d8\u91cf\uff0c\u4f46\u8bf7\u8c28\u614e\u4f7f\u7528\u6b64\u529f\u80fd\u3002\u4ee5\u4e0b\u793a\u4f8b\u5bf9\u4e00\u4e2a\u53ef\u53d8\u53d8\u91cf\u6267\u884c ++ \u64cd\u4f5c\uff1a<\/p>\n<pre>$mutableVar = 42;\n\n$func = function () use (&amp;$mutableVar) {\n    $mutableVar++;\n};<\/pre>\n<p>\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u503c\uff1a<\/p>\n<pre>$mutableVar = 42;\n\n$func = function () use ($mutableVar) {\n    return $mutableVar + 1;\n};<\/pre>\n<p><strong>4. \u4f7f\u7528\u4e0d\u53ef\u9884\u6d4b\u7684\u526f\u4f5c\u7528<\/strong><\/p>\n<p>FP \u51fd\u6570\u5e94\u8be5\u907f\u514d\u4ea7\u751f\u4e0d\u53ef\u9884\u6d4b\u7684\u526f\u4f5c\u7528\uff08\u4f8b\u5982\uff0c\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u6216\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff09\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c06\u6587\u672c\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\uff1a<\/p>\n<pre>$func = function () {\n    echo \"Hello, world!\";\n};<\/pre>\n<p>\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7ed3\u679c\u7684\u503c\uff1a<\/p>\n<pre>$func = function () {\n    return \"Hello, world!\";\n};<\/pre>\n<p><strong>5. \u6ee5\u7528\u9012\u5f52<\/strong><\/p>\n<p>\u9012\u5f52\u53ef\u4ee5\u5728 FP \u4e2d\u5f88\u6709\u7528\uff0c\u4f46\u8bf7\u8c28\u614e\u4f7f\u7528\u5b83\u4eec\u3002\u6ee5\u7528\u9012\u5f52\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u6216\u6027\u80fd\u95ee\u9898\u3002\u4ee5\u4e0b\u793a\u4f8b\u4f7f\u7528\u9012\u5f52\u6765\u8ba1\u7b97\u9636\u4e58\uff1a<\/p>\n<pre>function factorial($n) {\n    if ($n &lt;= 1) {\n        return 1;\n    } else {\n        return $n * factorial($n - 1);\n    }\n}<\/pre>\n<p>\u5bf9\u4e8e\u8f83\u5927\u7684\u8f93\u5165\uff0c\u6b64\u51fd\u6570\u53ef\u80fd\u4f1a\u9012\u5f52\u5f97\u592a\u6df1\u800c\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5faa\u73af\u4ee3\u66ff\u9012\u5f52\uff1a<\/p>\n<pre>function factorial($n) {\n    $result = 1;\n    for ($i = 1; $i &lt;= $n; $i++) {\n        $result *= $i;\n    }\n    return $result;\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5728\u4f7f\u7528\u95ed\u5305\u5b9e\u73b0 FP \u65f6\u907f\u514d\u5e38\u89c1\u9519\u8bef\uff1a<\/p>\n<pre>\/\/ \u6b63\u786e\u58f0\u660e\u95ed\u5305\n$func = function ($x) {\n    return $x * 2;\n};\n\n\/\/ \u4e0d\u4f9d\u8d56\u5168\u5c40\u53d8\u91cf\n$arr = [1, 2, 3];\n\n$result = array_map(function ($x) {\n    return $x * 2;\n}, $arr);\n\n\/\/ \u4e0d\u4fee\u6539\u53ef\u53d8\u53d8\u91cf\n$mutableArr = [1, 2, 3];\n\n$result = array_map(function ($x) {\n    return $x + 1;\n}, $mutableArr);\n\n\/\/ \u907f\u514d\u4e0d\u53ef\u9884\u6d4b\u7684\u526f\u4f5c\u7528\n$data = [\"name\" =&gt; \"John\", \"age\" =&gt; 30];\n\n$result = array_map(function ($item) {\n    return strtoupper($item);\n}, $data);<\/pre>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6307\u5357\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u5e72\u51c0\u3001\u53ef\u7ef4\u62a4\u4e14\u9ad8\u6027\u80fd\u7684 PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u4ee3\u7801\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aphp\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u4e2d\u7684\u5e38\u89c1\u9519\u8bef\u662f\u4ec0\u4e48\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u5bf9\u8c61\u7f16\u7a0b\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u51fd\u6570\u5bf9\u8c61\u7f16\u7a0b\u6307\u5357\u4e2d\u7684\u5e38\u89c1\u9519&#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-202429","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202429","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=202429"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202429\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}