{"id":2520,"date":"2013-02-28T13:04:15","date_gmt":"2013-02-28T05:04:15","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2520"},"modified":"2013-02-28T13:04:15","modified_gmt":"2013-02-28T05:04:15","slug":"25%e6%9d%a1php%e5%bc%80%e5%8f%91%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2520.html","title":{"rendered":"25\u6761PHP\u5f00\u53d1\u6280\u5de7"},"content":{"rendered":"<p><span style=\"font-size: small;\">1. \u4e0d\u8981\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u5b9a\u4e49\u4e00\u4e2aROOT\u8def\u5f84<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4e0b\u9762\u7684\u4ee3\u7801\u5f88\u5e38\u89c1\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nrequire_once('..\/..\/lib\/some_class.php');\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u4e2a\u65b9\u6cd5\u6709\u8bf8\u591a\u7f3a\u9677\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small;\">\u5b83\u4f1a\u5148\u5728PHP\u7684include\u8def\u5f84\u4e2d\u67e5\u627e\uff0c\u63a5\u7740\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u67e5\u627e\uff0c\u56e0\u6b64\u4f1a\u68c0\u67e5\u8bb8\u591a\u76ee\u5f55\u3002<\/span><\/li>\n<li><span style=\"font-size: small;\">\u5982\u679c\u811a\u672c\u7531\u5176\u4ed6\u76ee\u5f55\u4e2d\u7684\u811a\u672c\u6240\u5f15\u7528\uff0c\u76ee\u5f55\u7684\u8c03\u6574\u4f1a\u5f15\u53d1\u95ee\u9898\u3002<\/span><\/li>\n<li><span style=\"font-size: small;\">\u5f53\u4ee5\u8ba1\u5212\u4efb\u52a1\u8fd0\u884c\u811a\u672c\u65f6\uff0c\u4ee5\u76f8\u5bf9\u8def\u5f84\u5f62\u5f0f\u53ef\u80fd\u65e0\u6cd5\u627e\u5230\u7236\u76ee\u5f55<\/span><\/li>\n<\/ul>\n<p><span style=\"font-size: small;\">\u6bd4\u8f83\u597d\u7684\u529e\u6cd5\u662f\u91c7\u7528\u7edd\u5bf9\u8def\u5f84\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ndefine('ROOT' , '\/var\/www\/project\/');\r\nrequire_once(ROOT . '..\/..\/lib\/some_class.php');\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5f53\u7136\uff0c \u8fd9\u91cc\u6709\u7edd\u5bf9\u8def\u5f84\u548c\u5e38\u91cf\u3002\u53ef\u4ee5\u518d\u6765\u6539\u8fdb\u4e00\u4e0b\u3002\u8003\u8651\u4f7f\u7528\u9b54\u672f\u5e38\u91cf\uff0c\u6bd4\u5982__FILE__,\u770b\u770b\u8fd9\u6837\u5982\u4f55\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ndefine('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME));\r\nrequire_once(ROOT . '..\/..\/lib\/some_class.php');\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u597d\u4e86\uff0c\u73b0\u5728\u53ef\u4ee5\u8fc1\u79fb\u4f60\u7684\u9879\u76ee\u5230\u4e0d\u540c\u7684\u76ee\u5f55\u4e86\uff0c\u6bd4\u5982\u8fc1\u79fb\u5230\u5728\u7ebf\u670d\u52a1\u5668\uff0c\u65e0\u9700\u505a\u4efb\u4f55\u6539\u52a8\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">2.\u4e0d\u518d\u4f7f\u7528require, include, require_once, include_once<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4f60\u7684\u4ee3\u7801\u9876\u90e8\u4f1a\u5f15\u7528\u5f88\u591a\uff0c\u6bd4\u5982\u7c7b\u5e93\u3001\u6587\u4ef6\u3001\u5c0f\u5de5\u5177\u4ee5\u53ca\u5176\u4ed6helper\u51fd\u6570\uff0c \u6bd4\u5982\u8fd9\u6837\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nrequire_once('lib\/Database.php');\r\nrequire_once('lib\/Mail.php');\r\nrequire_once('helpers\/utitlity_functions.php');\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u6837\u6709\u4e9b\u539f\u59cb\u4e86\u3002\u4ee3\u7801\u9700\u8981\u6709\u5f39\u6027\u3002\u52a8\u624b\u5199\u4e2a\u80fd\u66f4\u5bb9\u6613\u5f15\u7528\u7684helper\u51fd\u6570\u5427\u3002\u770b\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction load_class($class_name)\r\n{\r\n    \/\/path to the class file\r\n    $path = ROOT . '\/lib\/' . $class_name . '.php');\r\n\r\n    if(file_exists($path))\r\n    {\r\n        require_once( $path );\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5b83\u53ef\u4ee5\u5b8c\u6210\u4ee5\u4e0b\u5de5\u4f5c\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small;\">\u5728\u591a\u76ee\u5f55\u4e2d\u641c\u7d22\u76f8\u540c\u7684\u7c7b\u6587\u4ef6<\/span><\/li>\n<li><span style=\"font-size: small;\">\u5f53\u6539\u53d8\u5f15\u7528\u5e93\u76ee\u5f55\u65f6\u4f1a\u975e\u5e38\u5bb9\u6613\uff0c\u800c\u4e0d\u7528\u5230\u5904\u53bb\u4fee\u6539\u4ee3\u7801<\/span><\/li>\n<li><span style=\"font-size: small;\">\u5982\u679c\u9700\u8981\u5f15\u7528html\u5185\u5bb9\uff0c\u7a0d\u52a0\u4fee\u6539\u5c31\u6210\u4e86load_htm<\/span><\/li>\n<\/ul>\n<p><span style=\"font-size: small;\">3.\u5efa\u7acb\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u8c03\u8bd5\u73af\u5883<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9047\u5230\u95ee\u9898\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u8f93\u51faDB\u67e5\u8be2\u3001dump\u53d8\u91cf\u2026\u2026\uff0c\u95ee\u9898\u89e3\u51b3\u540e\u6211\u4eec\u4f1a\u6ce8\u91ca\u6389\u6216\u8005\u5220\u9664\u3002\u5176\u5b9e\u5e94\u8be5\u7559\u7740\u5b83\u4eec\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ndefine('ENVIRONMENT' , 'development');\r\n\r\nif(! $db-&gt;query( $query )\r\n{\r\n    if(ENVIRONMENT == 'development')\r\n    {\r\n        echo &quot;$query failed&quot;;\r\n    }\r\n    else\r\n    {\r\n        echo &quot;Database error. Please contact administrator&quot;;\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">4. \u7528Session\u4f20\u9001\u72b6\u6001\u6d88\u606f<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5728\u5b8c\u6210\u4e00\u4e9b\u4efb\u52a1\u540e\uff0c\u7cfb\u7edf\/\u5e94\u7528\u7a0b\u5e8f\u4f1a\u8fdb\u884c\u4e00\u4e9b\u6d88\u606f\u63d0\u793a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nif($wrong_username || $wrong_password)\r\n{\r\n    $msg = 'Invalid username or password';\r\n}\r\n?&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;?php echo $msg; ?&gt;\r\n&lt;form&gt;\r\n...\r\n&lt;\/form&gt;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u4e9b\u4ee3\u7801\u5f88\u5e38\u89c1\u3002\u4f46\u8fd9\u79cd\u65b9\u6cd5\u5b58\u5728\u5c40\u9650\u6027\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small;\">\u4e0d\u80fd\u4f20\u9012\u8df3\u8f6c\u5730\u5740\uff08\u6253\u7b97\u7528GET\u53c2\u6570\u4f20\u9012\uff1f\u4eb2\uff0c\u8be5\u5403\u836f\u2026\u2026\uff09<\/span><\/li>\n<li><span style=\"font-size: small;\">\u6d88\u606f\u8fc7\u591a\u65f6\u7ba1\u7406\u56f0\u96be<\/span><\/li>\n<\/ul>\n<p><span style=\"font-size: small;\"> \u6700\u597d\u7684\u529e\u6cd5\u662f\u7528Session\u4f20\u9012\uff0c\u5f53\u7136\uff0c\u8bf7\u8bb0\u5f97session_start\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction set_flash($msg)\r\n{\r\n    $_SESSION&#x5B;'message'] = $msg;\r\n}\r\n\r\nfunction get_flash()\r\n{\r\n    $msg = $_SESSION&#x5B;'message'];\r\n    unset($_SESSION&#x5B;'message']);\r\n    return $msg;\r\n}\r\n<\/pre>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nif($wrong_username || $wrong_password)\r\n{\r\n    set_flash('Invalid username or password');\r\n}\r\n?&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\nStatus is : &lt;?php echo get_flash(); ?&gt;\r\n&lt;form&gt;\r\n...\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><span style=\"font-size: small;\">5. \u5f39\u6027\u5316\u4f60\u7684\u51fd\u6570<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction add_to_cart($item_id , $qty)\r\n{\r\n    $_SESSION&#x5B;'cart']&#x5B;$item_id] = $qty;\r\n}\r\nadd_to_cart( 'IPHONE3' , 2 );\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u7528\u4e0a\u9762\u7684\u51fd\u6570\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u5546\u54c1\u3002\u5982\u679c\u9700\u8981\u6dfb\u52a0\u591a\u4ef6\uff0c\u6211\u4eec\u53c8\u8981\u65b0\u5efa\u4e00\u4e2a\u51fd\u6570\u4e48\uff1fNO\u3002 \u53ea\u9700\u201c\u5f39\u6027\u5316\u201d\u5373\u53ef\uff0c\u770b\u8fd9\u4e2a\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction add_to_cart($item_id , $qty)\r\n{\r\n    if(!is_array($item_id))\r\n    {\r\n        $_SESSION&#x5B;'cart']&#x5B;$item_id] = $qty;\r\n    }\r\n    else\r\n    {\r\n        foreach($item_id as $i_id =&gt; $qty)\r\n        {\r\n            $_SESSION&#x5B;'cart']&#x5B;$i_id] = $qty;\r\n        }\r\n    }\r\n}\r\nadd_to_cart( 'IPHONE3' , 2 );\r\nadd_to_cart( array('IPHONE3' =&gt; 2 , 'IPAD' =&gt; 5) );\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u73b0\u5728\uff0c\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u79cd\u7c7b\u578b\uff0c\u6b64\u65b9\u6cd5\u5728\u5f88\u591a\u5730\u65b9\u90fd\u53ef\u5e94\u7528\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">6. \u5ffd\u7565php\u7684\u6536\u5c3e\u6807\u8bb0<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5f53\u6536\u5c3e\u6807\u8bb0?&gt;\u4e4b\u540e\u6709\u989d\u5916\u7684\u5b57\u7b26\uff08\u6bd4\u5982\u7a7a\u683c\uff09\uff0c\u4f60\u6b64\u523b\u9700\u8981echo \u4e00\u4e2aimage\u6216pdf\uff0c\u6216\u8005\u73a9cookies\/sessions\uff0c \u4f60\u4f1a\u770b\u5230\u201dheaders already send\u201d error\u3002\u539f\u56e0\u5728\u4e8e\u989d\u5916\u7684\u5b57\u7b26\u88ab\u663e\u793a\u51fa\u6765\u4e86\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6d6a\u8d39\u6570\u5c0f\u65f6\u53bb\u5bfb\u627e\u8fd9\u4e9b\u201c\u989d\u5916\u5b57\u7b26\u201d\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u907f\u514d\u6b64\u95ee\u9898\u7684\u65b9\u6cd5\u5c31\u662f\uff0c\u8bf7\u5ffd\u7565\u6536\u5c3e\u6807\u8bb0?&gt;\uff0c\u597d\u591a\u4e86\u5427\uff1f<\/span><\/p>\n<p><span style=\"font-size: small;\">7. \u6536\u96c6\u6240\u6709\u8f93\u51fa\uff0c \u518d\u4e00\u6b21\u8f93\u51fa\u7ed9\u6d4f\u89c8\u5668<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8fd9\u73a9\u610f\u662f\u8f93\u51fa\u7f13\u51b2\u3002\u6bd4\u5982\u4f60\u9700\u8981\u7528\u591a\u4e2a\u51fd\u6570\u8f93\u51fa\u5185\u5bb9\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction print_header()\r\n{\r\n    echo &quot;&lt;div id='header'&gt;Site Log and Login links&lt;\/div&gt;&quot;;\r\n}\r\nfunction print_footer()\r\n{\r\n    echo &quot;&lt;div id='footer'&gt;Site was made by me&lt;\/div&gt;&quot;;\r\n}\r\nprint_header();\r\nfor($i = 0 ; $i &lt; 100; $i++)\r\n{\r\n    echo &quot;I is : $i &lt;br \/&gt;';\r\n}\r\nprint_footer();\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8003\u8651\u8fd9\u4e48\u505a\uff1a\u9996\u5148\u6536\u96c6\u6240\u6709\u8f93\u51fa\u5230\u4e00\u4e2a\u5730\u65b9\u3002\u53ef\u4ee5\u5b58\u5728\u53d8\u91cf\u91cc\uff0c\u4e5f\u53ef\u4ee5\u7528ob_start\/ob_end_clean. \u6539\u6539\u770b\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction print_header()\r\n{\r\n    $o = &quot;&lt;div id='header'&gt;Site Log and Login links&lt;\/div&gt;&quot;;\r\n    return $o;\r\n}\r\nfunction print_footer()\r\n{\r\n    $o = &quot;&lt;div id='footer'&gt;Site was made by me&lt;\/div&gt;&quot;;\r\n    return $o;\r\n}\r\necho print_header();\r\nfor($i = 0 ; $i &lt; 100; $i++)\r\n{\r\n    echo &quot;I is : $i &lt;br \/&gt;';\r\n}\r\necho print_footer();\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e3a\u4f55\u9700\u8981\u8f93\u51fa\u7f13\u51b2\uff1f<\/span><\/p>\n<p><span style=\"font-size: small;\">\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668\u4e4b\u524d\u53ef\u4ee5\u6539\u52a8\u8f93\u51fa\u3002 \u6bd4\u5982\u6587\u672c\/\u6b63\u5219\u66ff\u6362\uff0c\u6216\u8005\u52a0\u4e00\u4e9b\u989d\u5916\u7684html\u4ee3\u7801\uff0c\u6bd4\u5982 profiler\/debugger<\/span><br \/>\n<span style=\"font-size: small;\"> \u540c\u65f6\u8fdb\u884cphp\u5904\u7406\u4e0e\u8f93\u51fa\u662f\u4e2a\u574f\u4e60\u60ef\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">8. \u8f93\u51fa\u975ehtml\u5185\u5bb9\u65f6\uff0c\u901a\u8fc7header\u53d1\u9001\u6b63\u786e\u7684mime\u7c7b\u578b<\/span><\/p>\n<p><span style=\"font-size: small;\">xml\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nheader(&quot;content-type: text\/xml&quot;);\r\necho $xml;\r\n<\/pre>\n<p><span style=\"font-size: small;\">Javascript<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nheader(&quot;content-type: application\/x-javascript&quot;);\r\necho &quot;var a = 10&quot;;\r\n<\/pre>\n<p><span style=\"font-size: small;\">CSS<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nheader(&quot;content-type: text\/css&quot;);\r\necho &quot;#div id { background:#000; }&quot;;\r\n<\/pre>\n<p><span style=\"font-size: small;\">9. mysql\u8fde\u63a5\u65f6\u8bbe\u7f6e\u6b63\u786e\u7684\u5b57\u7b26\u7f16\u7801<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679cmysql\u8868\u4ee5unicode\/utf-8\u6b63\u786e\u5b58\u50a8\uff0cphpmyadmin\u4e2d\u4e5f\u53ef\u6b63\u786e\u663e\u793a\uff0c\u4f46\u8bfb\u53d6\u6570\u636e\u663e\u793a\u5728\u9875\u9762\u65f6\u4e71\u7801\u51fa\u73b0\uff0c\u95ee\u9898\u5219\u51fa\u5728mysql\u8fde\u63a5\u6574\u7406\u4e0a\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$c = mysqli_connect($host , $username, $password);\r\nmysqli_set_charset ( $c , 'UTF8' );\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5f53\u8fde\u63a5\u5230\u6570\u636e\u5e93\u65f6\uff0c\u8bbe\u7f6e\u6574\u7406\u5b57\u7b26\u96c6\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff0c\u5728\u5f00\u53d1\u591a\u8bed\u8a00\u7684\u9879\u76ee\u4e2d\u5c24\u4e3a\u91cd\u8981\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">10. \u4f7f\u7528htmlentitis\u8bbe\u7f6e\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u9009\u9879<\/span><\/p>\n<p><span style=\"font-size: small;\">PHP 5.4\u4e4b\u524d\u7684\u9ed8\u8ba4\u5b57\u7b26\u7f16\u7801\u662fISO-8859-1\uff0c\u65e0\u6cd5\u663e\u793a\u8bf8\u5982\u00c0 \u00e2\u7b49\u5b57\u7b26\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$value = htmlentities($this-&gt;value , ENT_QUOTES , 'UTF-8');\r\n<\/pre>\n<p><span style=\"font-size: small;\">PHP 5.4\u8d77\uff0c\u9ed8\u8ba4\u7f16\u7801\u662fUTF-8\uff0c\u8fd9\u5c06\u89e3\u51b3\u5927\u90e8\u5206\u95ee\u9898\u3002\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4e3a\u591a\u8bed\u79cd\uff0c\u8bf7\u6ce8\u610f\u8fd9\u91cc<\/span><\/p>\n<p><span style=\"font-size: small;\">11. \u4e0d\u8981\u4f7f\u7528gzip\u8f93\u51fa\uff0c\u8ba9apache\u53bb\u505a\u8fd9\u4e2a<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8003\u8651\u7528ob_gzhandler? \u522b\u8fd9\u4e48\u505a\uff0c\u8fd9\u6837\u6ca1\u4ec0\u4e48\u610f\u4e49\u3002\u4e0d\u8981\u62c5\u5fc3\u5728php\u4e0a\u5982\u4f55\u4f18\u5316\u670d\u52a1\u5668\u548c\u6d4f\u89c8\u5668\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u8f93\u3002\u5728Apache\u4e2d\u542f\u7528mod_gzip\u6216\u8005mod_deflate\u6765\u538b\u7f29\u5427\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">12. \u4f7f\u7528json_encode \u5728PHP\u4e2d\u6253\u5370javascript\u4ee3\u7801<\/span><\/p>\n<p><span style=\"font-size: small;\">\u6709\u65f6\u9700\u8981\u5728PHP\u4e2d\u52a8\u6001\u751f\u6210\u4e00\u4e9bjavascript\u4ee3\u7801\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nforeach($images as $image)\r\n{\r\n    $js_code .= &quot;'$image' ,&quot;;\r\n}\r\n$js_code = 'var images = &#x5B;' . $js_code . ']; ';\r\necho $js_code;\r\n\/\/Output is var images = &#x5B;'myself.png' ,'friends.png' ,'colleagues.png' ,];\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8bd5\u8bd5json_encode\u5427\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$images = array(\r\n'myself.png' , 'friends.png' , 'colleagues.png'\r\n);\r\n$js_code = 'var images = ' . json_encode($images);\r\necho $js_code;\r\n\/\/Output is : var images = &#x5B;&quot;myself.png&quot;,&quot;friends.png&quot;,&quot;colleagues.png&quot;]\r\n<\/pre>\n<p><span style=\"font-size: small;\">13. \u5728\u5199\u6587\u4ef6\u4e4b\u524d\u8bf7\u5148\u68c0\u67e5\u76ee\u5f55\u662f\u5426\u53ef\u5199\u5165<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5199\u5165\u4efb\u4f55\u6587\u4ef6\u4e4b\u524d\uff0c\u8bf7\u786e\u8ba4\u8be5\u6587\u4ef6\u6240\u5728\u76ee\u5f55\u662f\u5426\u53ef\u5199\uff0c\u5982\u4e0d\u53ef\u5199\uff0c\u63d0\u793a\u9519\u8bef\u4fe1\u606f\u3002\u8fd9\u4f1a\u5e2e\u4f60\u8282\u7701\u65e0\u6570\u201c\u8c03\u8bd5\u201d\u65f6\u95f4\u3002\u5f53\u4f60\u5728linux\u4e0b\u5e72\u6d3b\u65f6\uff0c\u76ee\u5f55\u4e0d\u80fd\u88ab\u5199\u5165\u3001\u4e0d\u80fd\u8bfb\u53d6\u6587\u4ef6\u65f6\u8981\u9996\u5148\u8003\u8651\u76ee\u5f55\u6743\u9650\u95ee\u9898\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u786e\u4fdd\u4f60\u7684\u7a0b\u5e8f\u5728\u6700\u77ed\u65f6\u95f4\u5185\uff0c\u53ef\u4ee5\u5c3d\u91cf\u667a\u80fd\u5316\u5730\u62a5\u544a\u51fa\u6700\u91cd\u8981\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$contents = &quot;All the content&quot;;\r\n$file_path = &quot;\/var\/www\/project\/content.txt&quot;;\r\nfile_put_contents($file_path , $contents);\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4ee3\u7801\u6ca1\u95ee\u9898\uff0c \u4f46\u53ef\u80fd\u4f1a\u6709\u4e9b\u95f4\u63a5\u95ee\u9898\u4ea7\u751f\u3002File_put_contents\u5931\u8d25\u7684\u53ef\u80fd\u539f\u56e0\u5982\u4e0b\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small;\">\u7236\u76ee\u5f55\u4e0d\u5b58\u5728<\/span><\/li>\n<li><span style=\"font-size: small;\">\u76ee\u5f55\u5b58\u5728\uff0c\u4f46\u4e0d\u53ef\u5199<\/span><\/li>\n<li><span style=\"font-size: small;\">\u6587\u4ef6\u88ab\u9501\u5b9a<\/span><\/li>\n<\/ul>\n<p><span style=\"font-size: small;\"> \u56e0\u6b64\uff0c\u6700\u597d\u5728\u5199\u5165\u6587\u4ef6\u4e4b\u524d\u5148\u8fdb\u884c\u68c0\u6d4b\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$contents = &quot;All the content&quot;;\r\n$dir = '\/var\/www\/project';\r\n$file_path = $dir . &quot;\/content.txt&quot;;\r\nif(is_writable($dir))\r\n{\r\n    file_put_contents($file_path , $contents);\r\n}\r\nelse\r\n{\r\n    die(&quot;Directory $dir is not writable, or does not exist. Please check&quot;);\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u6837\u505a\u7684\u8bdd\uff0c\u5f53\u6587\u4ef6\u5199\u5165\u5931\u8d25\u65f6\u4f60\u4f1a\u77e5\u9053\u51c6\u786e\u7684\u4fe1\u606f\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">14. \u66f4\u6539\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u7684\u6587\u4ef6\u6743\u9650<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5f53\u4f60\u5728linux\u73af\u5883\u4e0b\u5de5\u4f5c\u65f6\uff0c\u6743\u9650\u5904\u7406\u4f1a\u6d6a\u8d39\u5f88\u591a\u65f6\u95f4\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u6587\u4ef6\u540e\uff0c\u8fdb\u884cchmod\u4ee5\u786e\u4fdd\u5916\u90e8\u53ef\u4ee5\u8bbf\u95ee\u3002\u5426\u5219\u4f1a\u5e26\u6765\u5f88\u591a\u9ebb\u70e6\u3002\u4f8b\u5982\uff0c\u751f\u6210\u7684\u6587\u4ef6\u7531\u201cPHP\u201d\u7528\u6237\u6240\u521b\u5efa\uff0c\u800c\u60a8\u5f00\u53d1\u65f6\u662f\u53e6\u4e00\u4e2a\u7528\u6237\uff0c\u7cfb\u7edf\u4f1a\u7981\u6b62\u60a8\u8bbf\u95ee\u6216\u6253\u5f00\u6587\u4ef6\uff0c\u4e4b\u540e\u4f60\u53ef\u80fd\u9700\u8981\u53d6\u5f97root\u6743\u9650\u518d\u53d8\u66f4\u6587\u4ef6\u6743\u9650\u2026\u2026<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ Read and write for owner, read for everybody else\r\nchmod(&quot;\/somedir\/somefile&quot;, 0644);\r\n\/\/ Everything for owner, read and execute for others\r\nchmod(&quot;\/somedir\/somefile&quot;, 0755);\r\n<\/pre>\n<p><span style=\"font-size: small;\">15. \u4e0d\u8981\u901a\u8fc7\u68c0\u67e5\u63d0\u4ea4\u6309\u94ae\u7684\u503c\u6765\u5224\u65ad\u8868\u5355\u63d0\u4ea4<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nif($_POST&#x5B;'submit'] == 'Save')\r\n{\r\n    \/\/Save the things\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u8d77\u6765\u7684\u786e\u6ca1\u4ec0\u4e48\u9519\u3002\u4f46\uff0c\u5f53\u4f60\u7684\u7a0b\u5e8f\u662f\u591a\u8bed\u8a00\u65f6\uff0c\u5c31\u4e0d\u4e00\u5b9a\u53ebSave\u4e86\uff0c\u8fd9\u65f6\u600e\u4e48\u5224\u65ad\uff1f\u6240\u4ee5\uff0c\u4e0d\u8981\u4f9d\u8d56\u63d0\u4ea4\u6309\u94ae\u7684\u503c\u4e86\uff0c\u8fd9\u4e48\u505a\u5427\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nif( $_SERVER&#x5B;'REQUEST_METHOD'] == 'POST' and isset($_POST&#x5B;'submit']) )\r\n{\r\n    \/\/Save the things\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">16. \u8003\u8651\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u9759\u6001\u53d8\u91cf<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/Delay for some time\r\nfunction delay()\r\n{\r\n    $sync_delay = get_option('sync_delay');\r\n    echo &quot;&lt;br \/&gt;Delaying for $sync_delay seconds...&quot;;\r\n    sleep($sync_delay);\r\n    echo &quot;Done &lt;br \/&gt;&quot;;\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u4e4b\u540e\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/Delay for some time\r\nfunction delay()\r\n{\r\n    static $sync_delay = null;\r\n    if($sync_delay == null)\r\n    {\r\n        $sync_delay = get_option('sync_delay');\r\n    }\r\n    echo &quot;&lt;br \/&gt;Delaying for $sync_delay seconds...&quot;;\r\n    sleep($sync_delay);\r\n    echo &quot;Done &lt;br \/&gt;&quot;;\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">17. \u4e0d\u8981\u76f4\u63a5\u4f7f\u7528$_SESSION\u53d8\u91cf<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$_SESSION&#x5B;'username'] = $username;\r\n$username = $_SESSION&#x5B;'username'];\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u719f\u6089\u5427\uff1f\u4f46\u8fd9\u4e48\u505a\u6709\u95ee\u9898\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679c\u5728\u76f8\u540c\u57df\u4e0b\u8fd0\u884c\u591a\u4e2a\u7a0b\u5e8f\uff0csession\u53d8\u91cf\u53ef\u80fd\u4f1a\u51b2\u7a81\uff0c 2\u4e2a\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u80fd\u8bbe\u7f6e\u4e86\u76f8\u540ckey\u7684session\u53d8\u91cf\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u56e0\u6b64\uff0c\u7528wrapper\u51fd\u6570\u6307\u5b9a\u4e00\u4e0bkey\u5427\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\ndefine('APP_ID' , 'cichui.com');\r\n\/\/Function to get a session variable\r\nfunction session_get($key)\r\n{\r\n    $k = APP_ID . '.' . $key;\r\n    if(isset($_SESSION&#x5B;$k]))\r\n    {\r\n        return $_SESSION&#x5B;$k];\r\n    }\r\n    return false;\r\n}\r\n\/\/Function set the session variable\r\nfunction session_set($key , $value)\r\n{\r\n    $k = APP_ID . '.' . $key;\r\n    $_SESSION&#x5B;$k] = $value;\r\n    return true;\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">18. \u5c06\u8f85\u52a9\u51fd\u6570(utility helper functions)\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4f60\u53ef\u80fd\u6709\u5f88\u591a\u50cf\u8fd9\u6837\u7684\u8f85\u52a9\u51fd\u6570\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction utility_a()\r\n{\r\n\/\/This function does a utility thing like string processing\r\n}\r\nfunction utility_b()\r\n{\r\n\/\/This function does nother utility thing like database processing\r\n}\r\nfunction utility_c()\r\n{\r\n\/\/This function is ...\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4f60\u53ef\u4ee5\u8003\u8651\u628a\u4ed6\u4eec\u5c01\u88c5\u6210\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nclass Utility\r\n{\r\n    public static function utility_a()\r\n    {\r\n    }\r\n    public static function utility_b()\r\n    {\r\n    }\r\n    public static function utility_c()\r\n    {\r\n    }\r\n}\r\n\/\/and call them as\r\n$a = Utility::utility_a();\r\n$b = Utility::utility_b();\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u8fd9\u6709\u4e00\u4e2a\u660e\u663e\u7684\u597d\u5904\u662f\uff0c\u4e0d\u4f1a\u548cPHP\u81ea\u5e26\u51fd\u6570\u547d\u540d\u51b2\u7a81\u3002\u53e6\u4e00\u4e2a\u89d2\u5ea6\u770b\uff0c\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5185\u5efa\u7acb\u591a\u4e2a\u7248\u672c\uff0c\u4e0d\u4f1a\u6709\u4efb\u4f55\u51b2\u7a81\u3002\u53ea\u662f\u6700\u57fa\u672c\u7684\u5c01\u88c5\uff0c \u6ca1\u522b\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">19. \u4e00\u4e9b\u611a\u8822\u7684\u5c0f\u6280\u5de7<\/span><\/p>\n<ul>\n<li><span style=\"font-size: small;\">\u7528echo\u4ee3\u66ffprint<\/span><\/li>\n<li><span style=\"font-size: small;\">\u9664\u975e\u7edd\u5bf9\u5fc5\u8981\uff0c\u8bf7\u7528str_replace\u4ee3\u66ffpreg_replace<\/span><\/li>\n<li><span style=\"font-size: small;\">\u4e0d\u8981\u4f7f\u7528\u77ed\u6807\u8bb0(<\/span><span style=\"font-size: small;\"> \u7b80\u5355\u5b57\u7b26\u4e32\u4f7f\u7528\u5355\u5f15\u53f7<\/span><\/li>\n<li><span style=\"font-size: small;\">\u6c38\u8fdc\u8bb0\u5f97\u5728header\u8df3\u8f6c\u540eexit<\/span><\/li>\n<li><span style=\"font-size: small;\">\u6c38\u8fdc\u4e0d\u8981\u5728for\u5faa\u73af\u63a7\u5236\u884c\u91cc\u8c03\u7528\u51fd\u6570<\/span><\/li>\n<li><span style=\"font-size: small;\">isset\u6bd4strlen\u5feb<\/span><\/li>\n<li><span style=\"font-size: small;\">\u5728\u5faa\u73af\u6216if-else\u4ee3\u7801\u5757\u4e2d\u8bf7\u575a\u6301\u4f7f\u7528\u5927\u62ec\u53f7{} \uff08\u5373\u4f7f\u4e00\u884c\uff09\u3002\u4e0d\u8981\u5c1d\u8bd5\u901a\u8fc7\u201c\u5403\u6389\u8bed\u6cd5\u201d\u800c\u8ba9\u4f60\u7684\u4ee3\u7801\u53d8\u77ed\uff0c\u8bf7\u8ba9\u4f60\u7684\u903b\u8f91\u66f4\u77ed\u4e00\u4e9b\u3002<\/span><\/li>\n<li><span style=\"font-size: small;\">\u4f7f\u7528\u8bed\u6cd5\u9ad8\u4eae\u7684\u7f16\u8f91\u5668\uff0c\u4ee3\u7801\u9ad8\u4eae\u6709\u52a9\u4e8e\u5e2e\u4f60\u51cf\u5c11\u9519\u8bef<\/span><\/li>\n<\/ul>\n<p><span style=\"font-size: small;\">20. \u4f7f\u7528array_map\u5feb\u901f\u5904\u7406\u6570\u7ec4<\/span><\/p>\n<p><span style=\"font-size: small;\">\u60f3\u6e05\u7406(trim)\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff1f\u65b0\u624b\u4e00\u822c\u4f1a\u8fd9\u6837\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nforeach($arr as $c =&gt; $v)\r\n{\r\n    $arr&#x5B;$c] = trim($v);\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u66f4\u6e05\u723d\u7684\u505a\u6cd5\u662f\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$arr = array_map('trim' , $arr);\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6b64\u51fd\u6570\u4f1a\u5c06trim\u5e94\u7528\u4e8e\u6240\u6709$arr\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u53e6\u4e00\u4e2a\u7c7b\u4f3c\u7684\u51fd\u6570\u662farray_walk\uff0c\u5177\u4f53\u8bf7\u53c2\u89c1PHP\u5e2e\u52a9\u6587\u6863\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">21. \u4f7f\u7528PHP filters\u6269\u5c55\u9a8c\u8bc1\u6570\u636e<\/span><\/p>\n<p><span style=\"font-size: small;\">\u4f60\u7528\u6b63\u5219\u505a\u8fc7\u6570\u636e\u6821\u9a8c\u5427\uff1f\u6bd4\u5982email, ip\u5730\u5740\u7b49\u7b49\u2026\u2026\u662f\u5f97\uff0c\u6bcf\u4e2a\u4eba\u90fd\u505a\u8fc7\u8fd9\u4e9b\u3002 \u73b0\u5728\u8bd5\u8bd5\u8fd9\u4e2a\u2014\u2014PHP\u7684filters\u6269\u5c55\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nif (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {\r\n\/\/\u2026\r\n}\r\nif (filter_var($ip_a, FILTER_VALIDATE_IP)) {\r\n\/\/\u2026\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u9664\u6b64\u4ee5\u5916\uff0c\u8fd8\u6709\uff1aFILTER_VALIDATE_URL\uff0cFILTER_VALIDATE_REGEXP\u2026\u2026\u2026<\/span><\/p>\n<p><span style=\"font-size: small;\">22. \u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$amount = intval( $_GET&#x5B;'amount'] );\r\n$rate = (int) $_GET&#x5B;'rate'];\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5f3a\u7c7b\u578b\u8f6c\u6362\u662f\u4e2a\u597d\u4e60\u60ef\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">23. \u4f7f\u7528set_error_handler() \u5c06PHP\u9519\u8bef\u65e5\u5fd7\u5199\u5165\u6587\u4ef6<\/span><\/p>\n<p><span style=\"font-size: small;\">set_error_handler()\u53ef\u4ee5\u7528\u6765\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u9519\u8bef\u3002\u7528\u5b83\u628a\u9519\u8bef\u65e5\u5fd7\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u4e5f\u662f\u4e2a\u4e0d\u9519\u7684\u4e3b\u610f\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">24. \u5c0f\u5fc3\u5904\u7406\u5927\u6570\u7ec4<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5982\u679c\u4e00\u4e2a\u53d8\u91cf\u5b58\u6709\u5927\u578b\u6570\u7ec4\u6216\u8005\u5b57\u7b26\u4e32\uff0c\u8bf7\u5c0f\u5fc3\u5904\u7406\u3002\u901a\u5e38\u7684\u9519\u8bef\u662f\u521b\u5efa\u526f\u672c\u7136\u540e\u5185\u5b58\u8017\u5c3d\uff0c\u5f97\u5230\u4e00\u4e2a\u5185\u5b58\u8d85\u51fa\u7684\u81f4\u547d\u9519\u8bef\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$db_records_in_array_format; \/\/1000\u884c\uff0a20\u5217\uff0c\u6bcf\u884c\u81f3\u5c11100 \u5b57\u8282 , so total 1000 * 20 * 100 = 2MB\r\n$cc = $db_records_in_array_format; \/\/\u7528\u63892MB\r\nsome_function($cc); \/\/\u64e6\uff0c\u8fd8\u8981\u518d\u75282MB ?\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u4e0a\u9762\u7684\u4ee3\u7801\u662f\u666e\u901a\u7684CSV\u6587\u4ef6\u5bfc\u5165\uff08\u6216\u5bfc\u51fa\uff09\u3002\u8fd9\u4e48\u5e72\u811a\u672c\u53ef\u80fd\u4f1a\u8d85\u51fa\u5185\u5b58\u9650\u503c\u3002\u5c0f\u89c4\u6a21\u7684\u5f53\u7136\u6ca1\u6709\u95ee\u9898\uff0c\u5927\u6570\u7ec4\u65f6\u8fd8\u662f\u8981\u63d0\u9632\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8003\u8651\u5f15\u7528\u4f20\u53c2(by reference)\u5427\uff0c \u6216\u8005\u5b58\u50a8\u5230\u7c7b\u53d8\u91cf\u91cc\u3002<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$a = get_large_array();\r\npass_to_function(&amp;$a);\r\nclass A\r\n{\r\n    function first()\r\n    {\r\n        $this-&gt;a = get_large_array();\r\n        $this-&gt;pass_to_function();\r\n    }\r\n    function pass_to_function()\r\n    {\r\n    \/\/process $this-&gt;a\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5927\u6570\u7ec4\u53d8\u91cf\u7528\u6bd5\u8bb0\u5f97\u5c3d\u5feb\u6ce8\u9500\u6389(unset)\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">25. \u6574\u4e2a\u811a\u672c\u4e2d\u4f7f\u7528\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u8bf7\u786e\u4fdd\u60a8\u4f7f\u7528\u4e00\u4e2a\u8fde\u63a5\u3002\u5f00\u59cb\u6253\u5f00\u8fde\u63a5\u5e76\u5f00\u59cb\u4f7f\u7528\uff0c\u76f4\u5230\u7ed3\u675f\uff0c\u5e76\u5728\u7ed3\u675f\u65f6\u5173\u95ed\u8fde\u63a5\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u8bf7\u4e0d\u8981\u8fd9\u4e48\u505a\uff1a<\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction add_to_cart()\r\n{\r\n    $db = new Database();\r\n    $db-&gt;query(&quot;INSERT INTO cart .....&quot;);\r\n}\r\nfunction empty_cart()\r\n{\r\n    $db = new Database();\r\n    $db-&gt;query(&quot;DELETE FROM cart .....&quot;);\r\n}\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u591a\u6b21\u6570\u636e\u5e93\u8fde\u63a5\u5f88\u7cdf\u7cd5\uff0c\u7531\u4e8e\u6bcf\u6b21\u8fde\u63a5\u90fd\u9700\u8981\u6d88\u8017\u65f6\u95f4\u548c\u66f4\u591a\u5185\u5b58\uff0c\u5b83\u4eec\u4f1a\u8ba9\u6267\u884c\u65f6\u95f4\u53d8\u5f97\u66f4\u6162\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5355\u4ef6\u6a21\u5f0f(Singleton pattern)\u8fdb\u884c\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e0d\u8981\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u5b9a\u4e49\u4e00\u4e2aROOT\u8def\u5f84 \u4e0b\u9762\u7684\u4ee3\u7801\u5f88\u5e38\u89c1\uff1a require_once(&#8216;..\/..\/li [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-2520","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=2520"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2520\/revisions"}],"predecessor-version":[{"id":2521,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2520\/revisions\/2521"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}