{"id":47870,"date":"2024-12-02T08:55:38","date_gmt":"2024-12-02T00:55:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47870\/"},"modified":"2024-12-02T08:55:38","modified_gmt":"2024-12-02T00:55:38","slug":"php%e5%87%bd%e6%95%b0%e7%ae%97%e6%b3%95%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%b8%b8%e8%a7%81%e9%99%b7%e9%98%b1%e4%b8%8e%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47870\/","title":{"rendered":"php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h1>\n<p>\u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aphp\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p>PHP \u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5171\u540c\u5b50\u8868\u8fbe\u5f0f\u6d88\u9664\uff1a\u6d88\u9664\u91cd\u590d\u8ba1\u7b97\u3002\u5faa\u73af\u4f18\u5316\uff1a\u4f7f\u7528\u9ad8\u6548\u7684\u5faa\u73af\u7ed3\u6784\uff08foreach\u3001while\u3001do-while\uff09\u3002\u7b97\u6cd5\u9009\u62e9\uff1a\u6839\u636e\u95ee\u9898\u9009\u62e9\u9002\u5f53\u7684\u7b97\u6cd5\uff08\u8003\u8651\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff09\u3002\u6570\u636e\u7ed3\u6784\u9009\u62e9\uff1a\u6839\u636e\u6570\u636e\u6027\u8d28\u548c\u8bbf\u95ee\u6a21\u5f0f\u9009\u62e9\u6700\u4f18\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u6563\u5217\u8868\u3001\u6808\u3001\u961f\u5217\uff09\u3002\u7f13\u5b58\uff1a\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u6216\u6570\u636e\u5e93\u67e5\u8be2\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729940560671ccc503af65.jpg\" class=\"aligncenter\" title=\"php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" alt=\"php\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>PHP \u662f\u4e00\u79cd\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u8bed\u8a00\uff0c\u4f46\u4e3a\u4e86\u4f18\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u6027\u80fd\uff0c\u4e86\u89e3\u5e38\u89c1\u9677\u9631\u548c\u6700\u4f73\u5b9e\u8df5\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u63a2\u8ba8\u4e86 PHP \u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u7684\u5173\u952e\u6280\u672f\uff0c\u5e76\u63d0\u4f9b\u4e86\u5b9e\u9645\u6848\u4f8b\u6765\u6f14\u793a\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<p><strong>1. \u5171\u540c\u5b50\u8868\u8fbe\u5f0f\u6d88\u9664 (CEE)<\/strong><\/p>\n<p><strong>\u9677\u9631\uff1a<\/strong>\u672a\u6d88\u9664\u91cd\u590d\u7684\u5b50\u8868\u8fbe\u5f0f\uff0c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong>\u4f7f\u7528 PHP \u7684 [CEE \u4f18\u5316\u5668](https:\/\/www.php.net\/manual\/en\/language.optimizations.ce.php)\uff0c\u5b83\u53ef\u4ee5\u8bc6\u522b\u5e76\u6d88\u9664\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u5171\u540c\u5b50\u8868\u8fbe\u5f0f\u672a\u6d88\u9664\n$a = 1 + 2;\n$b = $a + 3;\n\/\/ \u5171\u540c\u5b50\u8868\u8fbe\u5f0f\u5df2\u6d88\u9664\n$a = 1 + 2;\n$b = $a;\n$b += 3;<\/pre>\n<p><strong>2. \u5faa\u73af\u4f18\u5316<\/strong><\/p>\n<p><strong>\u9677\u9631\uff1a<\/strong>\u4f7f\u7528\u4f4e\u6548\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u4f8b\u5982\u5d4c\u5957\u5faa\u73af\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong>\u4f7f\u7528\u9ad8\u6548\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u4f8b\u5982 foreach\u3001while \u548c do-while \u5faa\u73af\u3002\u907f\u514d\u5d4c\u5957\u5faa\u73af\uff0c\u5982\u679c\u53ef\u80fd\uff0c\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f4e\u6548\u7684\u5d4c\u5957\u5faa\u73af\nfor ($i = 0; $i &lt; 10; $i++) {\n  for ($j = 0; $j &lt; 10; $j++) {\n    \/\/ \u7701\u7565\u4ee3\u7801\n  }\n}\n\/\/ \u9ad8\u6548\u7684\u5355\u5c42\u5faa\u73af\nforeach (range(0, 10) as $i) {\n  foreach (range(0, 10) as $j) {\n    \/\/ \u7701\u7565\u4ee3\u7801\n  }\n}<\/pre>\n<p><strong>3. \u7b97\u6cd5\u9009\u62e9<\/strong><\/p>\n<p><strong>\u9677\u9631\uff1a<\/strong>\u672a\u9009\u62e9\u9002\u5f53\u7684\u7b97\u6cd5\u6765\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\uff0c\u5bfc\u81f4\u6548\u7387\u4f4e\u4e0b\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong>\u6839\u636e\u95ee\u9898\u7684\u89c4\u6a21\u548c\u9884\u671f\u8f93\u5165\uff0c\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u3002\u8003\u8651\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u5bf9\u4e8e\u5c0f\u6570\u636e\u96c6\uff0c\u4f7f\u7528\u7ebf\u6027\u641c\u7d22\u9ad8\u6548\n$array = [1, 2, 3, 4, 5];\n$target = 3;\nforeach ($array as $value) {\n  if ($value == $target) {\n    break;\n  }\n}\n\/\/ \u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u4e8c\u5206\u67e5\u627e\u66f4\u52a0\u9ad8\u6548\n$array = range(1, 10000);\n$target = 5000;\n$low = 0;\n$high = count($array) - 1;\nwhile ($low &lt;= $high) {\n  $mid = (int)(($low + $high) \/ 2);\n  if ($array[$mid] == $target) {\n    break;\n  } elseif ($array[$mid] &lt; $target) {\n    $low = $mid + 1;\n  } else {\n    $high = $mid - 1;\n  }\n}<\/pre>\n<p><strong>4. \u6570\u636e\u7ed3\u6784\u9009\u62e9<\/strong><\/p>\n<p><strong>\u9677\u9631\uff1a<\/strong>\u672a\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u6570\u636e\uff0c\u5bfc\u81f4\u67e5\u627e\u548c\u63d2\u5165\u64cd\u4f5c\u6548\u7387\u4f4e\u4e0b\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong>\u6839\u636e\u6570\u636e\u7684\u6027\u8d28\u548c\u8bbf\u95ee\u6a21\u5f0f\uff0c\u9009\u62e9\u6700\u4f73\u7684\u6570\u636e\u7ed3\u6784\u3002\u8003\u8651\u6570\u7ec4\u3001\u6563\u5217\u8868\u3001\u6808\u548c\u961f\u5217\u7b49\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u5bf9\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u9ad8\u6548\n$hash = [];\n$key = 'my_key';\n$value = 123;\n$hash[$key] = $value;\necho $hash[$key];  \/\/ \u67e5\u627e\u64cd\u4f5c\n\/\/ \u5bf9\u4e8e\u9700\u8981\u6709\u5e8f\u7684\u63d2\u5165\u548c\u5220\u9664\uff0c\u4f7f\u7528\u94fe\u8868\u9ad8\u6548\n$list = new LinkedList();\n$list-&gt;add(1);\n$list-&gt;add(2);\n$list-&gt;add(3);\necho $list-&gt;get(1);  \/\/ \u67e5\u627e\u64cd\u4f5c<\/pre>\n<p><strong>5. \u7f13\u5b58<\/strong><\/p>\n<p><strong>\u9677\u9631\uff1a<\/strong>\u91cd\u590d\u83b7\u53d6\u76f8\u540c\u7684\u6570\u636e\uff0c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5\uff1a<\/strong>\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u6765\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u7684\u8ba1\u7b97\u6216\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ \u4f7f\u7528 APC \u7f13\u5b58\u673a\u5236\u6765\u7f13\u5b58\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\napc_store('query_result', $result);\n$result = apc_fetch('query_result');<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u4f18\u5316 PHP \u51fd\u6570\u7684\u7b97\u6cd5\uff0c\u63d0\u9ad8\u4ee3\u7801\u6027\u80fd\u5e76\u7f29\u77ed\u8fd0\u884c\u65f6\u95f4\u3002\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u4e14\u53ef\u6269\u5c55\u7684 PHP \u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aphp\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5\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\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5 \u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587\u300aphp\u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5\u300b\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60\u6587\u7ae0\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~ PHP \u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5171\u540c\u5b50\u8868\u8fbe\u5f0f\u6d88\u9664\uff1a\u6d88\u9664\u91cd\u590d\u8ba1\u7b97\u3002\u5faa\u73af\u4f18\u5316\uff1a\u4f7f\u7528\u9ad8\u6548\u7684\u5faa\u73af\u7ed3\u6784\uff08foreach\u3001while\u3001do-while\uff09\u3002\u7b97\u6cd5\u9009\u62e9\uff1a\u6839\u636e\u95ee\u9898\u9009\u62e9\u9002\u5f53\u7684\u7b97\u6cd5\uff08\u8003\u8651\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff09\u3002\u6570\u636e\u7ed3\u6784\u9009\u62e9\uff1a\u6839\u636e\u6570\u636e\u6027\u8d28\u548c\u8bbf\u95ee\u6a21\u5f0f\u9009\u62e9\u6700\u4f18\u7684\u6570\u636e\u7ed3\u6784\uff08\u6570\u7ec4\u3001\u6563\u5217\u8868\u3001\u6808\u3001\u961f\u5217\uff09\u3002\u7f13\u5b58\uff1a\u5b58\u50a8\u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u6216\u6570\u636e\u5e93\u67e5\u8be2\u3002 PHP \u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u6280\u5de7\uff1a\u5e38\u89c1\u9677\u9631\u4e0e\u6700\u4f73\u5b9e\u8df5 \u5f15\u8a00 PHP \u662f\u4e00\u79cd\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u8bed\u8a00\uff0c\u4f46\u4e3a\u4e86\u4f18\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u6027\u80fd\uff0c\u4e86\u89e3\u5e38\u89c1\u9677\u9631\u548c\u6700\u4f73\u5b9e\u8df5\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u63a2\u8ba8\u4e86 PHP \u51fd\u6570\u7b97\u6cd5\u4f18\u5316\u7684\u5173\u952e\u6280\u672f\uff0c\u5e76\u63d0\u4f9b\u4e86\u5b9e\u9645\u6848\u4f8b\u6765\u6f14\u793a\u8fd9\u4e9b\u6280\u672f\u3002 1. \u5171\u540c\u5b50\u8868\u8fbe\u5f0f\u6d88\u9664 (CEE) \u9677\u9631\uff1a\u672a\u6d88\u9664\u91cd\u590d\u7684\u5b50\u8868\u8fbe\u5f0f\uff0c\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002 \u6700\u4f73\u5b9e\u8df5\uff1a\u4f7f\u7528 PHP \u7684 [CEE \u4f18\u5316\u5668](https:\/\/www.php.net\/manual\/en\/language.optimizations.ce.php)\uff0c\u5b83\u53ef\u4ee5\u8bc6\u522b\u5e76\u6d88\u9664\u91cd\u590d\u8ba1\u7b97\u3002 \u793a\u4f8b\uff1a \/\/ \u5171\u540c\u5b50\u8868\u8fbe\u5f0f\u672a\u6d88\u9664 $a = 1 + 2; $b = $a + 3; \/\/ \u5171\u540c\u5b50\u8868\u8fbe\u5f0f\u5df2\u6d88\u9664 $a = 1 + 2; $b = $a; $b += 3; 2. \u5faa\u73af\u4f18\u5316 \u9677\u9631\uff1a\u4f7f\u7528\u4f4e\u6548\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u4f8b\u5982\u5d4c\u5957\u5faa\u73af\u3002 \u6700\u4f73\u5b9e\u8df5\uff1a\u4f7f\u7528\u9ad8\u6548\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u4f8b\u5982 foreach\u3001while \u548c do-while \u5faa\u73af\u3002\u907f\u514d\u5d4c\u5957\u5faa\u73af\uff0c\u5982\u679c\u53ef\u80fd\uff0c\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u89e3\u51b3\u95ee\u9898\u3002 \u793a\u4f8b\uff1a [&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-47870","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47870","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=47870"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47870\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}