{"id":47413,"date":"2024-12-02T16:28:14","date_gmt":"2024-12-02T08:28:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47413\/"},"modified":"2024-12-02T16:28:14","modified_gmt":"2024-12-02T08:28:14","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%95%e7%94%a8%e5%8f%82%e6%95%b0%e7%9a%84%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47413\/","title":{"rendered":"PHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef<\/h1>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u5f15\u7528\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u4f20\u9012\u7684\u53d8\u91cf\uff0c\u4f46\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u5e38\u89c1\u9519\u8bef\u5305\u62ec\uff1a\u7701\u7565 &amp; \u7b26\u53f7\uff0c\u5bfc\u81f4\u5bf9\u6570\u7ec4\u7684\u4fee\u6539\u65e0\u6548\u3002\u591a\u6b21\u4f20\u9012\u5f15\u7528\u53c2\u6570\uff0c\u5bfc\u81f4\u591a\u6b21\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u3002\u8fd4\u56de\u5f15\u7528\uff0c\u4f7f\u5916\u90e8\u5bf9\u6570\u7ec4\u7684\u4fee\u6539\u5f71\u54cd\u539f\u59cb\u53d8\u91cf\u3002\u5bf9\u5173\u8054\u6570\u7ec4\u4f7f\u7528\u5f15\u7528\uff0c\u5bfc\u81f4\u4fee\u6539\u952e\u503c\u4f46\u4e0d\u4fee\u6539\u952e\u672c\u8eab\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\uff0c\u8bf7\u4ed4\u7ec6\u68c0\u67e5\u51fd\u6570\u7b7e\u540d\uff0c\u8c28\u614e\u4f20\u9012\u5f15\u7528\uff0c\u8fd4\u56de\u526f\u672c\uff0c\u5e76\u4f7f\u7528\u5e38\u91cf\u4f20\u9012\u4e0d\u53ef\u53d8\u53d8\u91cf\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729917153671c70e1d9e9f.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef<\/strong><\/p>\n<p>\u5f15\u7528\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u663e\u5f0f\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u53d8\u91cf\u3002\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u540e\u679c\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u5e38\u72af\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5e38\u89c1\u7684\u9519\u8bef\uff1a<\/strong><\/p>\n<p><strong>1. \u7701\u7565 <code>&amp;<\/code> \u7b26\u53f7\uff1a<\/strong><\/p>\n<pre>function modifyArray(array $array) {\n    $array[] = 10;\n}\n\n$a = array(1, 2, 3);\nmodifyArray($a);\n\necho $a[3]; \/\/ \u8f93\u51fa: \u9519\u8bef\uff0c\u7d22\u5f15 3 \u4e0d\u5b58\u5728<\/pre>\n<p>\u6b64\u4ee3\u7801\u56e0\u672a\u5728\u6570\u7ec4\u53c2\u6570\u524d\u6dfb\u52a0 <code>&amp;<\/code> \u7b26\u53f7\u800c\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p><strong>2. \u591a\u6b21\u4f20\u9012\u5f15\u7528\u53c2\u6570\uff1a<\/strong><\/p>\n<pre>function modifyString(&amp;$string) {\n    $string = \"changed\";\n}\n\n$string1 = \"original\";\nmodifyString($string1);\nmodifyString($string1);\n\necho $string1; \/\/ \u8f93\u51fa: changed\uff0c\u4f46\u5b9e\u9645\u4e0a\u5e94\u8be5\u4e3a original<\/pre>\n<p>\u8fd9\u91cc\u4f20\u9012\u4e86\u4e00\u4e2a\u5f15\u7528\u53c2\u6570\uff0c\u7136\u540e\u53c8\u4f20\u9012\u4e86\u5b83\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u540e\u7684\u503c\u3002\u8fd9\u4f1a\u5bfc\u81f4\u591a\u6b21\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u3002<\/p>\n<p><strong>3. \u8fd4\u56de\u5f15\u7528\uff1a<\/strong><\/p>\n<pre>function &amp;getArray() {\n    return array(1, 2, 3);\n}\n\n$a = &amp;getArray();\n$a[] = 10;\n\necho $a[3]; \/\/ \u8f93\u51fa: 10<\/pre>\n<p>\u6b64\u51fd\u6570\u8fd4\u56de\u6570\u7ec4\u7684\u5f15\u7528\uff0c\u5bfc\u81f4\u5916\u90e8\u5bf9\u6570\u7ec4\u7684\u4fee\u6539\u5f71\u54cd\u5230\u539f\u59cb\u53d8\u91cf\u3002<\/p>\n<p><strong>4. \u5bf9\u5173\u8054\u6570\u7ec4\u4f7f\u7528\u5f15\u7528\uff1a<\/strong><\/p>\n<pre>function modifyAssocArray(array &amp;$array) {\n    $array['foo'] = 'bar';\n}\n\n$array = array('foo' =&gt; 'baz');\nmodifyAssocArray($array);\n\necho $array['foo']; \/\/ \u8f93\u51fa: bar\uff0c\u5b83\u5e94\u8be5\u662f baz<\/pre>\n<p>\u5bf9\u5173\u8054\u6570\u7ec4\u4f7f\u7528\u5f15\u7528\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u56e0\u4e3a\u952e\u7684\u503c\u4f1a\u88ab\u4fee\u6539\uff0c\u4f46\u952e\u672c\u8eab\u4ecd\u6307\u5411\u539f\u59cb\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u901a\u8fc7\u5f15\u7528\u4fee\u6539\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\uff1a<\/p>\n<pre>class MyClass {\n    public $name;\n\n    public function __construct($name) {\n        $this-&gt;name = $name;\n    }\n}\n\nfunction changeName(MyClass &amp;$object, $ newName) {\n    $object-&gt;name = $newName;\n}\n\n$obj = new MyClass('Bob');\nchangeName($obj, 'Alice');\n\necho $obj-&gt;name; \/\/ \u8f93\u51fa: Alice<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>changeName()<\/code> \u51fd\u6570\u4fee\u6539\u5bf9\u8c61\u7684 <code>name<\/code> \u5c5e\u6027\uff0c\u56e0\u4e3a\u4f20\u9012\u7684\u662f\u4e00\u4e2a\u5f15\u7528\u3002<\/p>\n<p><strong>\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\u7684\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u4ed4\u7ec6\u68c0\u67e5\u51fd\u6570\u7b7e\u540d\uff1a<\/strong>\u786e\u4fdd\u5728\u9700\u8981\u65f6\u4f7f\u7528 <code>&amp;<\/code> \u7b26\u53f7\u4f20\u9012\u5f15\u7528\uff0c\u907f\u514d\u5728\u4e0d\u9700\u8981\u65f6\u4f7f\u7528\u5b83\u3002<\/li>\n<li><strong>\u8c28\u614e\u4f20\u9012\u5f15\u7528\uff1a<\/strong>\u4ec5\u5728\u9700\u8981\u663e\u5f0f\u4fee\u6539\u53d8\u91cf\u65f6\u4f20\u9012\u5f15\u7528\u3002<\/li>\n<li><strong>\u8fd4\u56de\u526f\u672c\uff1a<\/strong>\u907f\u514d\u5c06\u5f15\u7528\u8fd4\u56de\u7ed9\u51fd\u6570\u5916\u90e8\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5e38\u91cf\uff1a<\/strong>\u8003\u8651\u5c06\u4e0d\u53ef\u53d8\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u4ee5\u907f\u514d\u610f\u5916\u4fee\u6539\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp,\u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u5f15\u7528\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u4fee\u6539\u4f20\u9012\u7684\u53d8\u91cf\uff0c\u4f46\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u5e38\u89c1\u9519\u8bef\u5305\u62ec\uff1a\u7701\u7565 &amp; \u7b26\u53f7\uff0c\u5bfc\u81f4\u5bf9\u6570\u7ec4\u7684\u4fee\u6539\u65e0\u6548\u3002\u591a\u6b21\u4f20\u9012\u5f15\u7528\u53c2\u6570\uff0c\u5bfc\u81f4\u591a\u6b21\u4fee\u6539\u5916\u90e8\u53d8\u91cf\u3002\u8fd4\u56de\u5f15\u7528\uff0c\u4f7f\u5916\u90e8\u5bf9\u6570\u7ec4\u7684\u4fee\u6539\u5f71\u54cd\u539f\u59cb\u53d8\u91cf\u3002\u5bf9\u5173\u8054\u6570\u7ec4\u4f7f\u7528\u5f15\u7528\uff0c\u5bfc\u81f4\u4fee\u6539\u952e\u503c\u4f46\u4e0d\u4fee\u6539\u952e\u672c\u8eab\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\uff0c\u8bf7\u4ed4\u7ec6\u68c0\u67e5\u51fd\u6570\u7b7e\u540d\uff0c\u8c28\u614e\u4f20\u9012\u5f15\u7528\uff0c\u8fd4\u56de\u526f\u672c\uff0c\u5e76\u4f7f\u7528\u5e38\u91cf\u4f20\u9012\u4e0d\u53ef\u53d8\u53d8\u91cf\u3002 PHP \u51fd\u6570\u5f15\u7528\u53c2\u6570\u7684\u5e38\u89c1\u9519\u8bef \u5f15\u7528\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u663e\u5f0f\u4fee\u6539\u4f20\u9012\u7ed9\u5b83\u7684\u53d8\u91cf\u3002\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u540e\u679c\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u5e38\u72af\u7684\u9519\u8bef\u3002 \u5e38\u89c1\u7684\u9519\u8bef\uff1a 1. \u7701\u7565 &amp; \u7b26\u53f7\uff1a function modifyArray(array $array) { $array[] = 10; } $a = array(1, 2, 3); modifyArray($a); echo $a[3]; \/\/ \u8f93\u51fa: \u9519\u8bef\uff0c\u7d22\u5f15 3 \u4e0d\u5b58\u5728 \u6b64\u4ee3\u7801\u56e0\u672a\u5728\u6570\u7ec4\u53c2\u6570\u524d\u6dfb\u52a0 &amp; \u7b26\u53f7\u800c\u5bfc\u81f4\u9519\u8bef\u3002 2. \u591a\u6b21\u4f20\u9012\u5f15\u7528\u53c2\u6570\uff1a function modifyString(&amp;$string) { $string = &#8220;changed&#8221;; } $string1 = &#8220;original&#8221;; modifyString($string1); modifyString($string1); echo $string1; [&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-47413","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47413","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=47413"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47413\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}