{"id":202443,"date":"2025-05-13T11:25:48","date_gmt":"2025-05-13T03:25:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202443\/"},"modified":"2025-05-13T11:25:48","modified_gmt":"2025-05-13T03:25:48","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%e7%8e%b0%e5%ae%9e%e4%b8%96%e7%95%8c%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202443\/","title":{"rendered":"PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u5e94\u7528\u573a\u666f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/h1>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u5e94\u7528\u573a\u666f\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241017\/172912577467105d8ed9da7.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><br \/>\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5b83\u5f3a\u8c03\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u548c\u7eaf\u51fd\u6570\u3002\u5728 PHP \u4e2d\uff0c\u51fd\u6570\u5f0f\u7f16\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u6d4b\u8bd5\u6027\u3002<\/p>\n<p><strong>1. \u4e0d\u53ef\u53d8\u6570\u636e<\/strong><br \/>\u4e0d\u53ef\u53d8\u6570\u636e\u662f\u6307\u4e00\u65e6\u521b\u5efa\u540e\u5c31\u4e0d\u80fd\u6539\u53d8\u7684\u6570\u636e\u3002\u8fd9\u6709\u52a9\u4e8e\u9632\u6b62\u610f\u5916\u6570\u636e\u4fee\u6539\u548c\u7ade\u4e89\u6761\u4ef6\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>const<\/code>\u3001<code>readonly<\/code> \u548c <code>immutable<\/code> \u5173\u952e\u5b57\u58f0\u660e\u4e0d\u53ef\u53d8\u6570\u636e\u3002<\/p>\n<pre>const PI = 3.14; \/\/ \u4e0d\u53ef\u53d8\u5e38\u91cf\nreadonly array $cities = ['London', 'Paris', 'New York']; \/\/ \u4e0d\u53ef\u53d8\u6570\u7ec4<\/pre>\n<p><strong>2. \u7eaf\u51fd\u6570<\/strong><br \/>\u7eaf\u51fd\u6570\u662f\u6307\u5176\u8f93\u51fa\u4ec5\u53d6\u51b3\u4e8e\u5176\u8f93\u5165\uff0c\u5e76\u4e14\u4e0d\u4ea7\u751f\u4efb\u4f55\u526f\u4f5c\u7528\uff08\u4f8b\u5982\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff09\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>fn<\/code> \u7bad\u5934\u51fd\u6570\u6216 <code>use<\/code> \u58f0\u660e\u6765\u521b\u5efa\u7eaf\u51fd\u6570\u3002<\/p>\n<pre>$double = fn($x) =&gt; $x * 2; \/\/ \u7eaf\u51fd\u6570<\/pre>\n<p><strong>3. \u4e00\u7b49\u51fd\u6570<\/strong><br \/>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u5176\u4ed6\u51fd\u6570\u7684\u53c2\u6570\u6216\u8fd4\u56de\u503c\u3002PHP \u652f\u6301\u4e00\u7b49\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u5c06\u51fd\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u4e2d\u5d4c\u5957\u51fd\u6570\u3002<\/p>\n<pre>\/\/ \u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\n$map = function(array $data, callable $callback) {\n  foreach ($data as $key =&gt; $value) {\n    $data[$key] = $callback($value);\n  }\n  return $data;\n};\n\n\/\/ \u5c06\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\n$filter = function(callable $callback) {\n  return function(array $data) use ($callback) {\n    $result = [];\n    foreach ($data as $key =&gt; $value) {\n      if ($callback($value)) {\n        $result[$key] = $value;\n      }\n    }\n    return $result;\n  };\n};<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u6570\u636e\u5904\u7406\u7ba1\u9053<\/strong><br \/>\u51fd\u6570\u5f0f\u7f16\u7a0b\u5728\u6570\u636e\u5904\u7406\u7ba1\u9053\u4e2d\u7279\u522b\u6709\u7528\u3002\u7ba1\u9053\u53ef\u4ee5\u5c06\u590d\u6742\u7684\u4efb\u52a1\u5206\u89e3\u6210\u4e00\u7cfb\u5217\u66f4\u5c0f\u7684\u3001\u66f4\u7b80\u5355\u7684\u6b65\u9aa4\u3002<\/p>\n<pre>\/\/ \u83b7\u53d6\u5947\u6570\n$oddNumbers = range(1, 10)\n  -&gt;filter(fn($x) =&gt; $x % 2) \/\/ \u8fc7\u6ee4\u5668\n  -&gt;toArray(); \/\/ \u8f6c\u6362\u6210\u6570\u7ec4\n\n\/\/ \u6bcf\u4e2a\u5947\u6570\u5e73\u65b9\n$squaredOddNumbers = $oddNumbers\n  -&gt;map(fn($x) =&gt; $x * $x) \/\/ \u53d8\u6362\u5668\n  -&gt;toArray();\n\n\/\/ \u6c42\u548c\u5e73\u65b9\u5947\u6570\n$sumOfSquaredOddNumbers = array_reduce($squaredOddNumbers, fn($carry, $x) =&gt; $carry + $x);<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><br \/>\u51fd\u6570\u5f0f\u7f16\u7a0b\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7f16\u5199\u66f4\u5e72\u51c0\u3001\u66f4\u5065\u58ee\u7684 PHP \u4ee3\u7801\u3002\u901a\u8fc7\u4f7f\u7528\u4e0d\u53ef\u53d8\u6570\u636e\u3001\u7eaf\u51fd\u6570\u548c\u4e00\u7b49\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u6d4b\u8bd5\u6027\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u73b0\u5b9e\u4e16\u754c\u4e2d\u7684\u5e94\u7528\u573a\u666f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp,\u51fd\u6570\u5f0f\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u73b0\u5b9e\u4e16\u754c&#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-202443","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202443","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=202443"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202443\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}