{"id":46768,"date":"2024-12-02T15:01:19","date_gmt":"2024-12-02T07:01:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46768\/"},"modified":"2024-12-02T15:01:19","modified_gmt":"2024-12-02T07:01:19","slug":"php%e5%af%b9%e5%9d%97%e4%bd%9c%e7%94%a8%e5%9f%9f%e5%92%8c%e5%85%a8%e5%b1%80%e4%bd%9c%e7%94%a8%e5%9f%9f%e7%9a%84%e6%9c%80%e6%96%b0%e6%94%b9%e8%bf%9b%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46768\/","title":{"rendered":"PHP\u5bf9\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u5bf9\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb\u6709\u54ea\u4e9b\uff1f<\/h1>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP\u5bf9\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb\u6709\u54ea\u4e9b\uff1f\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p>PHP 8 \u6539\u8fdb\u4e86\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u5904\u7406\uff1a\u5757\u4f5c\u7528\u57df\uff1a\u901a\u8fc7 use \u8bed\u53e5\u4ece\u7236\u4f5c\u7528\u57df\u5f15\u5165\u53d8\u91cf\uff0c\u660e\u786e\u8bbf\u95ee\u95ed\u5305\u5185\u7684\u53d8\u91cf\u3002\u5168\u5c40\u4f5c\u7528\u57df\uff1a\u901a\u8fc7 global \u5173\u952e\u5b57\u660e\u786e\u58f0\u660e\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u9632\u6b62\u610f\u5916\u66f4\u6539\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729845284671b582457dac.jpg\" class=\"aligncenter\" title=\"PHP\u5bf9\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb\u6709\u54ea\u4e9b\uff1f\u63d2\u56fe\" alt=\"PHP\u5bf9\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb\u6709\u54ea\u4e9b\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u4e2d\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb<\/strong><\/p>\n<p>PHP 8 \u5f15\u5165\u4e86\u91cd\u5927\u6539\u8fdb\uff0c\u63d0\u5347\u4e86\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u5904\u7406\u7684\u6e05\u6670\u5ea6\u548c\u4fbf\u5229\u6027\u3002\u8fd9\u4e9b\u6539\u8fdb\u8ba9\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u53d8\u5f97\u66f4\u8f7b\u677e\uff0c\u540c\u65f6\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u5757\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u5757\u4f5c\u7528\u57df\u6307\u7684\u662f\u53ea\u5728\u4ee3\u7801\u5757\u5185\u53ef\u8bbf\u95ee\u7684\u53d8\u91cf\u3002PHP 8 \u4e4b\u524d\uff0c\u533f\u540d\u51fd\u6570\u6216 Closure \u5185\u90e8\u7684\u53d8\u91cf\u53ea\u80fd\u5728\u8fd9\u4e9b\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u3002\u73b0\u5728\uff0c\u901a\u8fc7\u95ed\u5305\u7684 <code>use<\/code> \u8bed\u53e5\uff0c\u53ef\u4ee5\u660e\u786e\u58f0\u660e\u8981\u4ece\u7236\u4f5c\u7528\u57df\u5f15\u5165\u7684\u53d8\u91cf\u3002<\/p>\n<pre>$a = 10;\n$result = array_map(function($x) use($a) {\n    return $x + $a;\n}, [1, 2, 3]);<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf <code>$a<\/code> \u901a\u8fc7 <code>use<\/code> \u8bed\u53e5\u4ece\u7236\u4f5c\u7528\u57df\u4f20\u5165\u95ed\u5305\u4e2d\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u8bed\u53e5\uff0c\u95ed\u5305\u5c06\u65e0\u6cd5\u8bbf\u95ee <code>$a<\/code>\u3002<\/p>\n<p><strong>\u5168\u5c40\u4f5c\u7528\u57df<\/strong><\/p>\n<p>\u5168\u5c40\u4f5c\u7528\u57df\u662f\u6307\u5728\u6574\u4e2a\u811a\u672c\u8303\u56f4\u5185\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684\u53d8\u91cf\u3002PHP 8 \u5f15\u5165\u4e86\u65b0\u7684 <code>global<\/code> \u5173\u952e\u5b57\uff0c\u5b83\u5141\u8bb8\u5728\u51fd\u6570\u5185\u90e8\u660e\u786e\u58f0\u660e\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u610f\u5916\u66f4\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u4e5f\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<pre>$a = 10;\n\nfunction increment() {\n    global $a;\n    $a++;\n}<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u91cc\uff0c\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57\u660e\u786e\u58f0\u660e\u8981\u4f7f\u7528 <code>$a<\/code> \u5168\u5c40\u53d8\u91cf\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u5173\u952e\u5b57\uff0c\u51fd\u6570\u4e2d\u7684 <code>$a<\/code> \u5c06\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u4ece\u800c\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ PHP 7.4 \u53ca\u66f4\u65e9\u7248\u672c\nfunction calculate($input) {\n    $result = 0;\n    foreach ($input as $num) {\n        $result += $num;\n    }\n\n    return $result;\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>$result<\/code> \u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u5728 <code>calculate<\/code> \u51fd\u6570\u5185\u53ef\u89c1\u3002\u5982\u679c\u6709\u591a\u4e2a\u7c7b\u4f3c\u7684\u51fd\u6570\u5728\u4e0d\u540c\u4f5c\u7528\u57df\u5185\u4f7f\u7528\u76f8\u540c\u7684\u53d8\u91cf\u540d\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6df7\u4e71\u548c\u4e0d\u6b63\u786e\u7684\u8ba1\u7b97\u3002<\/p>\n<pre>\/\/ PHP 8\nfunction calculate($input) {\n    static $result = 0;\n    foreach ($input as $num) {\n        $result += $num;\n    }\n\n    return $result;\n}<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528 <code>static<\/code> \u5173\u952e\u5b57\uff0c<code>$result<\/code> \u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u9759\u6001\u53d8\u91cf\uff0c\u5b83\u8de8\u6240\u6709\u8c03\u7528 <code>calculate<\/code> \u51fd\u6570\u7684\u8c03\u7528\u4fdd\u6301\u5176\u503c\u3002\u8fd9\u5141\u8bb8\u5728\u591a\u6b21\u8c03\u7528\u51fd\u6570\u65f6\u7d2f\u79ef\u7ed3\u679c\uff0c\u800c\u4e0d\u7528\u62c5\u5fc3\u8986\u76d6\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u4e2d\u4f7f\u7528\u7684\u503c\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>PHP 8 \u4e2d\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6539\u8fdb\u4e3a\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5de5\u5177\u6765\u7ba1\u7406\u53d8\u91cf\u7684\u53ef\u89c1\u6027\u548c\u4f5c\u7528\u57df\u3002\u901a\u8fc7\u4f7f\u7528 <code>use<\/code> \u548c <code>global<\/code> \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u66f4\u660e\u786e\u5730\u63a7\u5236\u53d8\u91cf\u7684\u8bbf\u95ee\uff0c\u4ece\u800c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP\u5bf9\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb\u6709\u54ea\u4e9b\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u5bf9\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb\u6709\u54ea\u4e9b\uff1f \u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898\u662f\u300aPHP\u5bf9\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb\u6709\u54ea\u4e9b\uff1f\u300b\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01 PHP 8 \u6539\u8fdb\u4e86\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u5904\u7406\uff1a\u5757\u4f5c\u7528\u57df\uff1a\u901a\u8fc7 use \u8bed\u53e5\u4ece\u7236\u4f5c\u7528\u57df\u5f15\u5165\u53d8\u91cf\uff0c\u660e\u786e\u8bbf\u95ee\u95ed\u5305\u5185\u7684\u53d8\u91cf\u3002\u5168\u5c40\u4f5c\u7528\u57df\uff1a\u901a\u8fc7 global \u5173\u952e\u5b57\u660e\u786e\u58f0\u660e\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u9632\u6b62\u610f\u5916\u66f4\u6539\u3002 PHP \u4e2d\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6700\u65b0\u6539\u8fdb PHP 8 \u5f15\u5165\u4e86\u91cd\u5927\u6539\u8fdb\uff0c\u63d0\u5347\u4e86\u5757\u4f5c\u7528\u57df\u548c\u5168\u5c40\u4f5c\u7528\u57df\u5904\u7406\u7684\u6e05\u6670\u5ea6\u548c\u4fbf\u5229\u6027\u3002\u8fd9\u4e9b\u6539\u8fdb\u8ba9\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u53d8\u5f97\u66f4\u8f7b\u677e\uff0c\u540c\u65f6\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u5b89\u5168\u6027\u3002 \u5757\u4f5c\u7528\u57df \u5728 PHP \u4e2d\uff0c\u5757\u4f5c\u7528\u57df\u6307\u7684\u662f\u53ea\u5728\u4ee3\u7801\u5757\u5185\u53ef\u8bbf\u95ee\u7684\u53d8\u91cf\u3002PHP 8 \u4e4b\u524d\uff0c\u533f\u540d\u51fd\u6570\u6216 Closure \u5185\u90e8\u7684\u53d8\u91cf\u53ea\u80fd\u5728\u8fd9\u4e9b\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u3002\u73b0\u5728\uff0c\u901a\u8fc7\u95ed\u5305\u7684 use \u8bed\u53e5\uff0c\u53ef\u4ee5\u660e\u786e\u58f0\u660e\u8981\u4ece\u7236\u4f5c\u7528\u57df\u5f15\u5165\u7684\u53d8\u91cf\u3002 $a = 10; $result = array_map(function($x) use($a) { return $x + $a; }, [1, 2, 3]); \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf $a \u901a\u8fc7 use \u8bed\u53e5\u4ece\u7236\u4f5c\u7528\u57df\u4f20\u5165\u95ed\u5305\u4e2d\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u4e2a\u8bed\u53e5\uff0c\u95ed\u5305\u5c06\u65e0\u6cd5\u8bbf\u95ee $a\u3002 \u5168\u5c40\u4f5c\u7528\u57df \u5168\u5c40\u4f5c\u7528\u57df\u662f\u6307\u5728\u6574\u4e2a\u811a\u672c\u8303\u56f4\u5185\u90fd\u53ef\u4ee5\u8bbf\u95ee\u7684\u53d8\u91cf\u3002PHP 8 \u5f15\u5165\u4e86\u65b0\u7684 global \u5173\u952e\u5b57\uff0c\u5b83\u5141\u8bb8\u5728\u51fd\u6570\u5185\u90e8\u660e\u786e\u58f0\u660e\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u610f\u5916\u66f4\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u4e5f\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002 $a = 10; [&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-46768","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46768","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=46768"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46768\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}