{"id":48212,"date":"2024-12-02T08:37:20","date_gmt":"2024-12-02T00:37:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48212\/"},"modified":"2024-12-02T08:37:20","modified_gmt":"2024-12-02T00:37:20","slug":"php-%e5%87%bd%e6%95%b0%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e6%8a%80%e5%b7%a7%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48212\/","title":{"rendered":"PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aPHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p>PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u5305\u62ec\uff1a\u7f13\u5b58\u51fd\u6570\u7ed3\u679c\u3001\u4f7f\u7528\u9884\u7f16\u8bd1\u51fd\u6570\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u8c03\u7528\u3001\u7ec6\u5316\u6570\u636e\u7c7b\u578b\u3002\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u65f6\u95f4\u548c\u541e\u5410\u91cf\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729999613671db2fd0f678.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3<\/strong><\/p>\n<p>PHP \u4e2d\u7684\u51fd\u6570\u6027\u80fd\u662f\u4e00\u4e2a\u7ecf\u5e38\u88ab\u5ffd\u89c6\u7684\u91cd\u8981\u56e0\u7d20\u3002\u901a\u8fc7\u4f18\u5316\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u65f6\u95f4\u548c\u541e\u5410\u91cf\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5404\u79cd PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u6848\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<p><strong>\u7f13\u5b58\u51fd\u6570\u7ed3\u679c<\/strong><\/p>\n<p>\u907f\u514d\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u51fd\u6570\u662f\u63d0\u9ad8\u6027\u80fd\u7684\u5173\u952e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982 <code>apc_add()<\/code> \u6216 <code>memcached<\/code> \u7b49\u6269\u5c55\u5c06\u51fd\u6570\u7ed3\u679c\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u3002\u5728\u540e\u7eed\u8c03\u7528\u4e2d\uff0c\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u6602\u8d35\u7684\u91cd\u65b0\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b 1:<\/strong> \u4e00\u4e2a\u83b7\u53d6\u6570\u636e\u5e93\u8bb0\u5f55\u7684\u51fd\u6570\uff1a<\/p>\n<pre>function getFromDB($id) {\n  $result = $db-&gt;query(\"SELECT * FROM table WHERE id = $id\");\n  return $result-&gt;fetch();\n}<\/pre>\n<p>\u4f18\u5316\uff1a<\/p>\n<pre>$cache = apc_add('FromDB_' . $id, getFromDB($id), 300);\nif ($cache !== false) {\n  return $cache;\n}<\/pre>\n<p><strong>\u4f7f\u7528\u9884\u7f16\u8bd1\u51fd\u6570<\/strong><\/p>\n<p>\u9884\u7f16\u8bd1\u51fd\u6570\u53ef\u4ee5\u6d88\u9664 PHP \u89e3\u6790\u51fd\u6570\u8c03\u7528\u7684\u5f00\u9500\u3002\u4f7f\u7528\u8bf8\u5982 <code>filter_var()<\/code> \u6216 <code>str_replace()<\/code> \u7b49\u5e26\u6709 <code>_filter<\/code> \u6216 <code>_replace<\/code> \u540e\u7f00\u7684\u51fd\u6570\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b 2:<\/strong> \u4e00\u4e2a\u8fc7\u6ee4\u5b57\u7b26\u4e32\u7684\u51fd\u6570\uff1a<\/p>\n<pre>function filterString($str) {\n  return filter_var($str, FILTER_SANITIZE_STRING);\n}<\/pre>\n<p>\u4f18\u5316\uff1a<\/p>\n<pre>function filterString($str) {\n  static $filterFunc = null;\n  if ($filterFunc === null) {\n    $filterFunc = filter_var('', FILTER_SANITIZE_STRING);\n  }\n  return $filterFunc($str);\n}<\/pre>\n<p><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u8c03\u7528<\/strong><\/p>\n<p>\u4ed4\u7ec6\u68c0\u67e5\u4f60\u7684\u4ee3\u7801\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5faa\u73af\u5e76\u91cd\u590d\u6267\u884c\u76f8\u540c\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u63d0\u53d6\u5230\u5faa\u73af\u4e4b\u5916\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b 3:<\/strong> \u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\uff1a<\/p>\n<pre>function squareArray($arr) {\n  foreach ($arr as $val) {\n    $result[] = square($val);\n  }\n  return $result;\n}<\/pre>\n<p>\u4f18\u5316\uff1a<\/p>\n<pre>function squareArray($arr) {\n  $squareFunc = function($val) {\n    return $val * $val;\n  };\n  return array_map($squareFunc, $arr);\n}<\/pre>\n<p><strong>\u7ec6\u5316\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<p>\u4f7f\u7528\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u7684\u51fd\u6570\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>is_int()<\/code> \u4ee3\u66ff <code>is_numeric()<\/code>\uff0c\u6216\u8005\u4f7f\u7528 <code>array_key_exists()<\/code> \u4ee3\u66ff <code>in_array()<\/code>\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b 4:<\/strong> \u68c0\u67e5\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u952e\uff1a<\/p>\n<pre>function keyExists($arr, $key) {\n  if (in_array($key, array_keys($arr))) {\n    return true;\n  }\n  return false;\n}<\/pre>\n<p>\u4f18\u5316\uff1a<\/p>\n<pre>function keyExists($arr, $key) {\n  return array_key_exists($key, $arr);\n}<\/pre>\n<p>\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 PHP \u51fd\u6570\u7684\u6027\u80fd\u3002\u8bb0\u4f4f\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u4e0d\u65ad\u8c03\u6574\u548c\u6539\u8fdb\u4ee5\u83b7\u5f97\u6700\u4f73\u7ed3\u679c\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aPHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3\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,\u6027\u80fd\u4f18\u5316\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3 \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aPHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u5305\u62ec\uff1a\u7f13\u5b58\u51fd\u6570\u7ed3\u679c\u3001\u4f7f\u7528\u9884\u7f16\u8bd1\u51fd\u6570\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u8c03\u7528\u3001\u7ec6\u5316\u6570\u636e\u7c7b\u578b\u3002\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u65f6\u95f4\u548c\u541e\u5410\u91cf\u3002 PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\u8be6\u89e3 PHP \u4e2d\u7684\u51fd\u6570\u6027\u80fd\u662f\u4e00\u4e2a\u7ecf\u5e38\u88ab\u5ffd\u89c6\u7684\u91cd\u8981\u56e0\u7d20\u3002\u901a\u8fc7\u4f18\u5316\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u65f6\u95f4\u548c\u541e\u5410\u91cf\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5404\u79cd PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff0c\u5e76\u63d0\u4f9b\u5b9e\u9645\u6848\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002 \u7f13\u5b58\u51fd\u6570\u7ed3\u679c \u907f\u514d\u91cd\u590d\u6267\u884c\u6602\u8d35\u7684\u51fd\u6570\u662f\u63d0\u9ad8\u6027\u80fd\u7684\u5173\u952e\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982 apc_add() \u6216 memcached \u7b49\u6269\u5c55\u5c06\u51fd\u6570\u7ed3\u679c\u5b58\u50a8\u5728\u7f13\u5b58\u4e2d\u3002\u5728\u540e\u7eed\u8c03\u7528\u4e2d\uff0c\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u6602\u8d35\u7684\u91cd\u65b0\u8ba1\u7b97\u3002 \u5b9e\u6218\u6848\u4f8b 1: \u4e00\u4e2a\u83b7\u53d6\u6570\u636e\u5e93\u8bb0\u5f55\u7684\u51fd\u6570\uff1a function getFromDB($id) { $result = $db-&gt;query(&#8220;SELECT * FROM table WHERE id = $id&#8221;); return $result-&gt;fetch(); } \u4f18\u5316\uff1a $cache = apc_add(&#8216;FromDB_&#8217; . $id, getFromDB($id), 300); if ($cache !== false) { return $cache; } \u4f7f\u7528\u9884\u7f16\u8bd1\u51fd\u6570 \u9884\u7f16\u8bd1\u51fd\u6570\u53ef\u4ee5\u6d88\u9664 [&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-48212","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48212","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=48212"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48212\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}