{"id":202431,"date":"2025-05-13T13:01:17","date_gmt":"2025-05-13T05:01:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202431\/"},"modified":"2025-05-13T13:01:17","modified_gmt":"2025-05-13T05:01:17","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%e5%a6%82%e4%bd%95%e5%9c%a8%e4%bd%a0%e7%9a%84%e9%a1%b9%e7%9b%ae%e4%b8%ad%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202431\/","title":{"rendered":"PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u5982\u4f55\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u5982\u4f55\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u5b9e\u73b0\uff1f<\/h1>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u5982\u4f55\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u5b9e\u73b0\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3001\u53ef\u6d4b\u8bd5\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u5b9e\u73b0\u6b65\u9aa4\u5305\u62ec\uff1a\u7406\u89e3\u7eaf\u7cb9\u51fd\u6570\u3001\u4e0d\u53ef\u53d8\u6570\u636e\u548c\u9ad8\u9636\u51fd\u6570\u7684\u6982\u5ff5\u3002\u4f7f\u7528\u5d4c\u5957\u51fd\u6570\u5c01\u88c5\u4ee3\u7801\u3002\u5229\u7528\u533f\u540d\u51fd\u6570\u548c\u95ed\u5305\u521b\u5efa\u52a8\u6001\u51fd\u6570\u3002\u91c7\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u907f\u514d\u6570\u636e\u4fee\u6539\u3002\u5b9e\u6218\u5e94\u7528\uff1a\u8fc7\u6ee4\u548c\u6620\u5c04\u6570\u7ec4\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241015\/1728993409670e5881d6821.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u5b9e\u73b0\u5728\u9879\u76ee\u4e2d\u7684\u5206\u6b65\u6559\u7a0b<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b (FP) \u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5b83\u5f3a\u8c03\u4f7f\u7528\u7eaf\u7cb9\u51fd\u6570\u548c\u4e0d\u53ef\u53d8\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3001\u53ef\u6d4b\u8bd5\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p><strong>\u7b2c\u4e00\u6b65\uff1a\u4e86\u89e3\u51fd\u6570\u5f0f\u7f16\u7a0b\u6982\u5ff5<\/strong><\/p>\n<ul>\n<li>\u7eaf\u7cb9\u51fd\u6570\uff1a\u8f93\u5165\u76f8\u540c\uff0c\u8f93\u51fa\u59cb\u7ec8\u76f8\u540c\uff0c\u4e14\u65e0\u526f\u4f5c\u7528\u3002<\/li>\n<li>\u4e0d\u53ef\u53d8\u6570\u636e\uff1a\u521b\u5efa\u540e\u65e0\u6cd5\u66f4\u6539\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<li>\u9ad8\u9636\u51fd\u6570\uff1a\u4f5c\u4e3a\u53c2\u6570\u63a5\u6536\u5176\u4ed6\u51fd\u6570\u5e76\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u7b2c\u4e8c\u6b65\uff1a\u4f7f\u7528\u5d4c\u5957\u51fd\u6570<\/strong><\/p>\n<p>\u5d4c\u5957\u51fd\u6570\u4f7f\u4f60\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u5b9a\u4e49\u5176\u4ed6\u51fd\u6570\uff0c\u8fd9\u6709\u52a9\u4e8e\u5c01\u88c5\u4ee3\u7801\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>function sumEven(int $n): int\n{\n    function isEven($num): bool\n    {\n        return $num % 2 === 0;\n    }\n\n    $sum = 0;\n    for ($i = 1; $i &lt;= $n; $i++) {\n        if (isEven($i)) {\n            $sum += $i;\n        }\n    }\n\n    return $sum;\n}<\/pre>\n<p><strong>\u7b2c\u4e09\u6b65\uff1a\u5229\u7528\u533f\u540d\u51fd\u6570\u548c\u95ed\u5305<\/strong><\/p>\n<p>\u533f\u540d\u51fd\u6570\u5141\u8bb8\u4f60\u521b\u5efa\u6ca1\u6709\u540d\u79f0\u7684\u51fd\u6570\uff0c\u95ed\u5305\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$adder = function ($a, $b) {\n    return $a + $b;\n};\n\n$result = $adder(1, 2); \/\/ \u8f93\u51fa\uff1a3<\/pre>\n<p><strong>\u7b2c\u56db\u6b65\uff1a\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>\u907f\u514d\u4f7f\u7528\u53ef\u53d8\u6570\u7ec4\u548c\u5bf9\u8c61\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\u4f7f\u7528\u4e0d\u53ef\u53d8\u5217\u8868 (SplFixedArray)\u3001\u6620\u5c04 (SplObjectStorage) \u548c\u96c6\u5408 (SplDoublyLinkedList)\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$list = new SplFixedArray(3);\n$list[0] = 1;\n$list[1] = 2;\n\/\/ $list[1] = 3; \/\/ Fatal Error: Cannot modify immutable array<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u8fc7\u6ee4\u5e76\u6620\u5c04\u6570\u7ec4<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u8fc7\u6ee4\u548c\u6620\u5c04\u6570\u7ec4\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>$names = ['John', 'Mary', 'Tom', 'Alice'];\n\n\/\/ \u8fc7\u6ee4\u6389\u957f\u5ea6\u5927\u4e8e 4 \u7684\u540d\u79f0\n$longNames = filter($names, function ($name) {\n    return strlen($name) &gt; 4;\n});\n\n\/\/ \u5c06\u540d\u79f0\u6620\u5c04\u4e3a\u5927\u5199\n$upperCaseNames = map($longNames, function ($name) {\n    return strtoupper($name);\n});\n\nprint_r($upperCaseNames); \/\/ \u8f93\u51fa\uff1a['JOHN', 'ALICE']<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u5c06\u51fd\u6570\u5f0f\u7f16\u7a0b\u6280\u672f\u878d\u5165\u5230\u4f60\u7684 PHP \u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u66f4\u4f18\u96c5\u3001\u53ef\u7ef4\u62a4\u4e14\u53ef\u6269\u5c55\u7684\u4ee3\u7801\u3002\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u5e76\u5229\u7528\u63d0\u4f9b\u7684\u5b9e\u6218\u6848\u4f8b\uff0c\u4f60\u53ef\u4ee5\u63d0\u5347\u4f60\u7684\u7f16\u7a0b\u6280\u80fd\u5e76\u6536\u83b7 FP \u7684\u597d\u5904\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u5982\u4f55\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u5b9e\u73b0\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u5982\u4f55\u5728\u4f60&#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-202431","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202431","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=202431"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202431\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}