{"id":47373,"date":"2024-12-02T17:19:32","date_gmt":"2024-12-02T09:19:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47373\/"},"modified":"2024-12-02T17:19:32","modified_gmt":"2024-12-02T09:19:32","slug":"php-%e5%87%bd%e6%95%b0%e4%b8%ad%e5%bc%95%e7%94%a8%e6%95%b0%e7%bb%84%e5%85%83%e7%b4%a0%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47373\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aPHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>\u5f15\u7528 PHP \u6570\u7ec4\u5143\u7d20\u65f6\u9700\u8981\u6ce8\u610f\uff1a\u4f20\u9012\u5f15\u7528\u65f6\u5e94\u4f7f\u7528 &#8220;&amp;&#8221; \u8fd0\u7b97\u7b26\uff1b\u5bf9\u4e8e\u5d4c\u5957\u6570\u7ec4\uff0c\u5e94\u4f20\u9012\u5d4c\u5957\u5143\u7d20\u5f15\u7528\uff1b\u4ec5\u652f\u6301\u4f20\u9012\u503c\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u4e14\u4e0d\u80fd\u4f20\u9012\u5bf9\u8c61\u6216\u591a\u7ef4\u6570\u7ec4\u7684\u5f15\u7528\u3002\u5728\u5b9e\u6218\u4e2d\uff0c\u5f15\u7528\u53ef\u7528\u4e8e\u4fee\u6539\u6570\u7ec4\u5143\u7d20\uff0c\u4f8b\u5982\u5728\u8868\u5355\u64cd\u4f5c\u4e2d\u5c06\u8f93\u5165\u503c\u6dfb\u52a0\u5230\u5916\u90e8\u6570\u7ec4\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729914083671c64e3552bf.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f15\u7528\u6765\u4fee\u6539\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u3002\u7136\u800c\uff0c\u5f53\u8bd5\u56fe\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879\u548c\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u4f20\u9012\u5f15\u7528<\/strong><\/p>\n<p>\u8981\u5f15\u7528\u6570\u7ec4\u5143\u7d20\uff0c\u9700\u8981\u4f7f\u7528 &#8220;&amp;&#8221; \u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>function addOne(&amp;$arr) {\n  $arr[0]++;\n}\n\n$arr = [1, 2, 3];\naddOne($arr);\n\necho $arr[0]; \/\/ \u8f93\u51fa 2<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f20\u9012\u4e86\u6570\u7ec4\u5f15\u7528\u7ed9\u51fd\u6570\uff0c\u56e0\u6b64\u5bf9\u5143\u7d20\u7684\u4fee\u6539\u5c06\u53cd\u5e94\u5728\u5916\u90e8\u6570\u7ec4\u4e0a\u3002<\/p>\n<p><strong>\u5d4c\u5957\u6570\u7ec4<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5d4c\u5957\u6570\u7ec4\uff0c\u4f7f\u7528\u5f15\u7528\u65f6\u9700\u8981\u6ce8\u610f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>function updateKey(&amp;$arr, $key) {\n  $arr[$key]['value'] = 'new value';\n}\n\n$arr = ['key' =&gt; ['value' =&gt; 'old value']];\nupdateKey($arr, 'key');\n\necho $arr['key']['value']; \/\/ \u8f93\u51fa 'new value'<\/pre>\n<p>\u5728\u5d4c\u5957\u6570\u7ec4\u7684\u60c5\u51b5\u4e0b\uff0c\u4f20\u9012\u5f15\u7528\u7684\u662f\u6700\u5916\u5c42\u6570\u7ec4\u3002\u8981\u4fee\u6539\u5d4c\u5957\u5143\u7d20\uff0c\u9700\u8981\u5c06\u5f15\u4f20\u9012\u7ed9\u5d4c\u5957\u5143\u7d20\u3002<\/p>\n<p><strong>\u5f15\u7528\u4f20\u9012\u9650\u5236<\/strong><\/p>\n<p>PHP \u4e2d\u5f15\u7528\u4f20\u9012\u53d7\u5230\u4ee5\u4e0b\u9650\u5236\uff1a<\/p>\n<ul>\n<li>\u53ea\u4f20\u9012\u503c\u7c7b\u578b\uff08\u5982 int\u3001float\u3001string \u7b49\uff09\u7684\u5f15\u7528\u3002<\/li>\n<li>\u4e0d\u53ef\u4f20\u9012\u5f15\u7528\u7ed9\u5bf9\u8c61\u3002<\/li>\n<li>\u53ea\u4f20\u9012\u4e00\u7ef4\u6570\u7ec4\u7684\u5f15\u7528\u3002\u591a\u7ef4\u6570\u7ec4\u5fc5\u987b\u4f20\u9012\u5f15\u7528\u94fe\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u5728 Form \u64cd\u4f5c\u4e2d\u4f7f\u7528\u5f15\u7528\u6765\u4fee\u6539\u6570\u7ec4\u5143\u7d20\uff1a<\/p>\n<pre>\/\/ \u7f51\u7ad9\u8868\u5355\necho '&lt;form action=\"handle_form.php\" method=\"post\"&gt;';\necho '&lt;input type=\"text\" name=\"username\"&gt;';\necho '&lt;input type=\"submit\"&gt;';\necho '&lt;\/form&gt;';\n\n\/\/ handle_form.php\n\/\/ \u83b7\u53d6\u8868\u5355\u6570\u636e\n$username = $_POST['username'];\n\n\/\/ \u6821\u9a8c\u7528\u6237\u540d\nif (empty($username)) {\n  $errors['username'] = '\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a';\n}\n\n\/\/ \u5982\u679c\u6ca1\u6709\u9519\u8bef\uff0c\u63d0\u4ea4\u6570\u636e\nif (empty($errors)) {\n  \/\/ \u4f7f\u7528\u5f15\u7528\u4fee\u6539\u5916\u90e8\u6570\u7ec4\n  $users[]=&amp;$username;\n\n  \/\/ \u4fdd\u5b58\u6570\u636e\n}\n\n\/\/ \u663e\u793a\u9519\u8bef\nif (!empty($errors)) {\n  \/\/ ...\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f15\u7528\u7528\u4e8e\u5c06\u8f93\u5165\u503c\u6dfb\u52a0\u5230\u5916\u90e8\u6570\u7ec4\uff0c\u65b9\u4fbf\u5728\u8868\u5355\u5904\u7406\u811a\u672c\u4e2d\u4fdd\u5b58\u6570\u636e\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879\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,\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879 \u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aPHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002 \u5f15\u7528 PHP \u6570\u7ec4\u5143\u7d20\u65f6\u9700\u8981\u6ce8\u610f\uff1a\u4f20\u9012\u5f15\u7528\u65f6\u5e94\u4f7f\u7528 &#8220;&amp;&#8221; \u8fd0\u7b97\u7b26\uff1b\u5bf9\u4e8e\u5d4c\u5957\u6570\u7ec4\uff0c\u5e94\u4f20\u9012\u5d4c\u5957\u5143\u7d20\u5f15\u7528\uff1b\u4ec5\u652f\u6301\u4f20\u9012\u503c\u7c7b\u578b\u7684\u5f15\u7528\uff0c\u4e14\u4e0d\u80fd\u4f20\u9012\u5bf9\u8c61\u6216\u591a\u7ef4\u6570\u7ec4\u7684\u5f15\u7528\u3002\u5728\u5b9e\u6218\u4e2d\uff0c\u5f15\u7528\u53ef\u7528\u4e8e\u4fee\u6539\u6570\u7ec4\u5143\u7d20\uff0c\u4f8b\u5982\u5728\u8868\u5355\u64cd\u4f5c\u4e2d\u5c06\u8f93\u5165\u503c\u6dfb\u52a0\u5230\u5916\u90e8\u6570\u7ec4\u3002 PHP \u51fd\u6570\u4e2d\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879 \u5f15\u8a00 \u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f15\u7528\u6765\u4fee\u6539\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u3002\u7136\u800c\uff0c\u5f53\u8bd5\u56fe\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u5f15\u7528\u6570\u7ec4\u5143\u7d20\u7684\u6ce8\u610f\u4e8b\u9879\u548c\u89e3\u51b3\u65b9\u6cd5\u3002 \u4f20\u9012\u5f15\u7528 \u8981\u5f15\u7528\u6570\u7ec4\u5143\u7d20\uff0c\u9700\u8981\u4f7f\u7528 &#8220;&amp;&#8221; \u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff1a function addOne(&amp;$arr) { $arr[0]++; } $arr = [1, 2, 3]; addOne($arr); echo $arr[0]; \/\/ \u8f93\u51fa 2 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f20\u9012\u4e86\u6570\u7ec4\u5f15\u7528\u7ed9\u51fd\u6570\uff0c\u56e0\u6b64\u5bf9\u5143\u7d20\u7684\u4fee\u6539\u5c06\u53cd\u5e94\u5728\u5916\u90e8\u6570\u7ec4\u4e0a\u3002 \u5d4c\u5957\u6570\u7ec4 \u5bf9\u4e8e\u5d4c\u5957\u6570\u7ec4\uff0c\u4f7f\u7528\u5f15\u7528\u65f6\u9700\u8981\u6ce8\u610f\u3002\u4f8b\u5982\uff1a function updateKey(&amp;$arr, $key) { $arr[$key][&#8216;value&#8217;] = &#8216;new value&#8217;; } $arr = [&#8216;key&#8217; =&gt; [&#8216;value&#8217; =&gt; &#8216;old [&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-47373","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47373","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=47373"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47373\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}