{"id":46970,"date":"2024-12-02T08:30:03","date_gmt":"2024-12-02T00:30:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46970\/"},"modified":"2024-12-02T08:30:03","modified_gmt":"2024-12-02T00:30:03","slug":"php-%e5%87%bd%e6%95%b0%e7%9a%84%e6%80%a7%e8%83%bd%e8%b0%83%e4%bc%98%e5%ae%9e%e6%93%8d%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46970\/","title":{"rendered":"PHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357\u300b<\/span>\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p>\u4f18\u5316 PHP \u51fd\u6570\u6027\u80fd\u7684\u6307\u5357\uff1a\u5206\u6790\u51fd\u6570\u6027\u80fd\uff0c\u627e\u51fa\u6267\u884c\u65f6\u95f4\u8fc7\u957f\u6216\u5185\u5b58\u6d88\u8017\u8fc7\u591a\u7684\u51fd\u6570\u3002\u5e94\u7528\u4f18\u5316\u6280\u672f\uff0c\u5982\uff1a\u4ee3\u7801\u5185\u8054\u53c2\u6570\u9884\u5904\u7406\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u7f13\u5b58\u7ed3\u679c<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729857754671b88da3d92a.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u51fd\u6570\u8c03\u4f18\u5bf9\u4e8e\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u5b9e\u6218\u6307\u5357\uff0c\u6307\u5bfc\u60a8\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u63d0\u9ad8\u6267\u884c\u901f\u5ea6\u3002<\/p>\n<p><strong>\u5206\u6790\u51fd\u6570\u6027\u80fd<\/strong><\/p>\n<ol>\n<li>\u4f7f\u7528 <code>xdebug<\/code> \u6269\u5c55\u6216 <code>php -f<\/code> \u9009\u9879\u8fd0\u884c PHP \u811a\u672c\uff0c\u4ee5\u67e5\u770b\u51fd\u6570\u6267\u884c\u65f6\u95f4\u548c\u5185\u5b58\u6d88\u8017\u3002<\/li>\n<li>\u67e5\u627e\u6267\u884c\u65f6\u95f4\u8fc7\u957f\u6216\u5185\u5b58\u6d88\u8017\u8fc7\u591a\u7684\u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><strong>\u4f18\u5316\u6280\u672f<\/strong><\/p>\n<p><strong>1. \u4ee3\u7801\u5185\u8054<\/strong><\/p>\n<p>\u5c06\u5c0f\u51fd\u6570\u5185\u8054\u5230\u8c03\u7528\u5b83\u4eec\u7684\u51fd\u6570\u4e2d\uff0c\u4ee5\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u3002<\/p>\n<pre>\/\/ \u672a\u4f18\u5316\nfunction my_function_x() {\n  echo 'Hello';\n}\n\nfunction my_function_y() {\n  my_function_x();\n}\n\n\/\/ \u4f18\u5316\u540e\nfunction my_function_y() {\n  echo 'Hello';\n}<\/pre>\n<p><strong>2. \u53c2\u6570\u9884\u5904\u7406<\/strong><\/p>\n<p>\u9884\u5904\u7406\u51fd\u6570\u53c2\u6570\uff0c\u4ee5\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<pre>\/\/ \u672a\u4f18\u5316\nfunction my_function($x, $y) {\n  return sqrt($x * $x + $y * $y);\n}\n\n\/\/ \u4f18\u5316\u540e\nfunction my_function($x, $y) {\n  $d = $x * $x + $y * $y;\n  return sqrt($d);\n}<\/pre>\n<p><strong>3. \u6570\u636e\u7ed3\u6784\u9009\u62e9<\/strong><\/p>\n<p>\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u4f18\u5316\u51fd\u6570\u7684\u6027\u80fd\u3002 \u4f8b\u5982\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u63d0\u9ad8\u952e\u503c\u68c0\u7d22\u901f\u5ea6\u3002<\/p>\n<p><strong>4. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af<\/strong><\/p>\n<p>\u4ec5\u5728\u5fc5\u8981\u65f6\u4f7f\u7528\u5faa\u73af\u3002 \u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u548c\u63d0\u524d\u9000\u51fa\u673a\u5236\u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8fed\u4ee3\u3002<\/p>\n<p><strong>5. \u7f13\u5b58\u7ed3\u679c<\/strong><\/p>\n<p>\u7f13\u5b58\u51fd\u6570\u7684\u4e2d\u95f4\u7ed3\u679c\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u8ba1\u7b97\u6570\u7ec4\u4e2d\u5143\u7d20\u548c\u7684\u51fd\u6570\uff1a<\/p>\n<pre>function sum_array($arr) {\n  $sum = 0;\n  foreach ($arr as $item) {\n    $sum += $item;\n  }\n  return $sum;\n}<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4f18\u5316\u6280\u672f\u63d0\u9ad8\u5176\u6027\u80fd\uff1a<\/p>\n<ul>\n<li>\u4ee3\u7801\u5185\u8054\uff1a\u8fd4\u56de\u6570\u7ec4\u5143\u7d20\u4e4b\u548c\uff0c\u800c\u65e0\u9700\u5faa\u73af\u3002<\/li>\n<li>\u6570\u636e\u7ed3\u6784\u9009\u62e9\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u54c8\u5e0c\u8868\uff0c\u4ee5\u63d0\u9ad8\u5143\u7d20\u68c0\u7d22\u901f\u5ea6\u3002<\/li>\n<li>\u7f13\u5b58\u7ed3\u679c\uff1a\u5c06\u6570\u7ec4\u548c\u7684\u7f13\u5b58\u5b58\u50a8\u5728\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u4e2d\uff0c\u4ee5\u540e\u4f7f\u7528\u65f6\u76f4\u63a5\u83b7\u53d6\u3002<\/li>\n<\/ul>\n<p>\u4f18\u5316\u540e\u7684\u51fd\u6570\uff1a<\/p>\n<pre>$cache = []; \/\/ \u7f13\u5b58\u6570\u7ec4\u548c\nfunction sum_array($arr) {\n  $key = md5(serialize($arr)); \/\/ \u54c8\u5e0c\u8868\u952e\n  if (array_key_exists($key, $cache)) {\n    return $cache[$key]; \/\/ \u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7ed3\u679c\n  }\n  $sum = array_sum($arr); \/\/ \u6c42\u548c\n  $cache[$key] = $sum; \/\/ \u5c06\u7ed3\u679c\u7f13\u5b58\n  return $sum;\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u5e94\u7528\u672c\u6587\u4e2d\u7684\u4f18\u5316\u6280\u672f\uff0c\u60a8\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 PHP \u51fd\u6570\u7684\u6027\u80fd\uff0c\u4ece\u800c\u6539\u5584\u5e94\u7528\u7a0b\u5e8f\u7684\u6574\u4f53\u901f\u5ea6\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357 \u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587\u300aPHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357\u300b\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0\u6587\u7ae0\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01 \u4f18\u5316 PHP \u51fd\u6570\u6027\u80fd\u7684\u6307\u5357\uff1a\u5206\u6790\u51fd\u6570\u6027\u80fd\uff0c\u627e\u51fa\u6267\u884c\u65f6\u95f4\u8fc7\u957f\u6216\u5185\u5b58\u6d88\u8017\u8fc7\u591a\u7684\u51fd\u6570\u3002\u5e94\u7528\u4f18\u5316\u6280\u672f\uff0c\u5982\uff1a\u4ee3\u7801\u5185\u8054\u53c2\u6570\u9884\u5904\u7406\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u7f13\u5b58\u7ed3\u679c PHP \u51fd\u6570\u7684\u6027\u80fd\u8c03\u4f18\u5b9e\u64cd\u6307\u5357 \u7b80\u4ecb PHP \u51fd\u6570\u8c03\u4f18\u5bf9\u4e8e\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63d0\u4f9b\u5b9e\u6218\u6307\u5357\uff0c\u6307\u5bfc\u60a8\u4f18\u5316 PHP \u51fd\u6570\u4ee5\u63d0\u9ad8\u6267\u884c\u901f\u5ea6\u3002 \u5206\u6790\u51fd\u6570\u6027\u80fd \u4f7f\u7528 xdebug \u6269\u5c55\u6216 php -f \u9009\u9879\u8fd0\u884c PHP \u811a\u672c\uff0c\u4ee5\u67e5\u770b\u51fd\u6570\u6267\u884c\u65f6\u95f4\u548c\u5185\u5b58\u6d88\u8017\u3002 \u67e5\u627e\u6267\u884c\u65f6\u95f4\u8fc7\u957f\u6216\u5185\u5b58\u6d88\u8017\u8fc7\u591a\u7684\u51fd\u6570\u3002 \u4f18\u5316\u6280\u672f 1. \u4ee3\u7801\u5185\u8054 \u5c06\u5c0f\u51fd\u6570\u5185\u8054\u5230\u8c03\u7528\u5b83\u4eec\u7684\u51fd\u6570\u4e2d\uff0c\u4ee5\u51cf\u5c11\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u3002 \/\/ \u672a\u4f18\u5316 function my_function_x() { echo &#8216;Hello&#8217;; } function my_function_y() { my_function_x(); } \/\/ \u4f18\u5316\u540e function my_function_y() { echo &#8216;Hello&#8217;; } 2. \u53c2\u6570\u9884\u5904\u7406 \u9884\u5904\u7406\u51fd\u6570\u53c2\u6570\uff0c\u4ee5\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u3002 \/\/ \u672a\u4f18\u5316 function [&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-46970","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46970","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=46970"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46970\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}