{"id":1070,"date":"2010-05-07T10:04:00","date_gmt":"2010-05-07T02:04:00","guid":{"rendered":""},"modified":"2013-11-25T20:49:09","modified_gmt":"2013-11-25T12:49:09","slug":"%e4%b9%9d%e4%b8%aaphp%e5%be%88%e6%9c%89%e7%94%a8%e7%9a%84%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1070.html","title":{"rendered":"\u4e5d\u4e2aPHP\u5f88\u6709\u7528\u7684\u529f\u80fd"},"content":{"rendered":"<p>\u4e0b\u9762\u662f\u4e5d\u4e2aPHP\u4e2d\u5f88\u6709\u7528\u7684\u529f\u80fd\uff0c\u4e0d\u77e5\u9053\u4f60\u7528\u8fc7\u4e86\u5417\uff1f<\/p>\n<p>1. \u51fd\u6570\u7684\u4efb\u610f\u6570\u76ee\u7684\u53c2\u6570<br \/>\n\u4f60\u53ef\u80fd\u77e5\u9053PHP\u5141\u8bb8\u4f60\u5b9a\u4e49\u4e00\u4e2a\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\u3002\u4f46\u4f60\u53ef\u80fd\u5e76\u4e0d\u77e5\u9053PHP\u8fd8\u5141\u8bb8\u4f60\u5b9a\u4e49\u4e00\u4e2a\u5b8c\u5168\u4efb\u610f\u7684\u53c2\u6570\u7684\u51fd\u6570<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u5411\u4f60\u5c55\u793a\u4e86\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ \u4e24\u4e2a\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\r\nfunction foo($arg1 = '', $arg2 = '') {\r\n\r\n echo &quot;arg1: $arg1\\n&quot;;\r\n echo &quot;arg2: $arg2\\n&quot;;\r\n\r\n}\r\n\r\nfoo('hello','world');\r\n\/* \u8f93\u51fa:\r\narg1: hello\r\narg2: world\r\n*\/\r\n\r\nfoo();\r\n\/* \u8f93\u51fa:\r\narg1:\r\narg2:\r\n*\/\r\n\u73b0\u5728\u6211\u4eec\u6765\u770b\u4e00\u770b\u4e00\u4e2a\u4e0d\u5b9a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u5176\u4f7f\u7528\u5230\u4e86?func_get_args()\u65b9\u6cd5\uff1a\r\n\r\n\/\/ \u662f\u7684\uff0c\u5f62\u53c2\u5217\u8868\u4e3a\u7a7a\r\nfunction foo() {\r\n\r\n \/\/ \u53d6\u5f97\u6240\u6709\u7684\u4f20\u5165\u53c2\u6570\u7684\u6570\u7ec4\r\n $args = func_get_args();\r\n\r\n foreach ($args as $k =&gt; $v) {\r\n  echo &quot;arg&quot;.($k+1).&quot;: $v\\n&quot;;\r\n }\r\n\r\n}\r\n\r\nfoo();\r\n\/* \u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u8f93\u51fa *\/\r\n\r\nfoo('hello');\r\n\/* \u8f93\u51fa\r\narg1: hello\r\n*\/\r\n\r\nfoo('hello', 'world', 'again');\r\n\/* \u8f93\u51fa\r\narg1: hello\r\narg2: world\r\narg3: again\r\n*\/\r\n<\/pre>\n<p>2. \u4f7f\u7528 Glob() \u67e5\u627e\u6587\u4ef6<br \/>\n\u5f88\u591aPHP\u7684\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u6bd4\u8f83\u957f\u7684\u81ea\u89e3\u91ca\u7684\u51fd\u6570\u540d\uff0c\u4f46\u662f\uff0c\u5f53\u4f60\u770b\u5230?glob() \u7684\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u5e76\u4e0d\u77e5\u9053\u8fd9\u4e2a\u51fd\u6570\u662f\u7528\u6765\u5e72\u4ec0\u4e48\u7684\uff0c\u9664\u975e\u4f60\u5bf9\u5b83\u5df2\u7ecf\u5f88\u719f\u6089\u4e86\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u8fd9\u4e2a\u51fd\u6570\u5c31\u597d?scandir() \u4e00\u6837\uff0c\u5176\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u6587\u4ef6\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ \u53d6\u5f97\u6240\u6709\u7684\u540e\u7f00\u4e3aPHP\u7684\u6587\u4ef6\r\n$files = glob('*.php');\r\n\r\nprint_r($files);\r\n\/* \u8f93\u51fa:\r\nArray\r\n(\r\n    &#x5B;0] =&gt; phptest.php\r\n    &#x5B;1] =&gt; pi.php\r\n    &#x5B;2] =&gt; post_output.php\r\n    &#x5B;3] =&gt; test.php\r\n)\r\n*\/\r\n\u4f60\u8fd8\u53ef\u4ee5\u67e5\u627e\u591a\u79cd\u540e\u7f00\u540d\r\n\r\n\/\/ \u53d6PHP\u6587\u4ef6\u548cTXT\u6587\u4ef6\r\n$files = glob('*.{php,txt}', GLOB_BRACE);\r\n\r\nprint_r($files);\r\n\/* \u8f93\u51fa:\r\nArray\r\n(\r\n    &#x5B;0] =&gt; phptest.php\r\n    &#x5B;1] =&gt; pi.php\r\n    &#x5B;2] =&gt; post_output.php\r\n    &#x5B;3] =&gt; test.php\r\n    &#x5B;4] =&gt; log.txt\r\n    &#x5B;5] =&gt; test.txt\r\n)\r\n*\/\r\n\u4f60\u8fd8\u53ef\u4ee5\u52a0\u4e0a\u8def\u5f84\uff1a\r\n\r\n$files = glob('..\/images\/a*.jpg');\r\n\r\nprint_r($files);\r\n\/* \u8f93\u51fa:\r\nArray\r\n(\r\n    &#x5B;0] =&gt; ..\/images\/apple.jpg\r\n    &#x5B;1] =&gt; ..\/images\/art.jpg\r\n)\r\n*\/\r\n\u5982\u679c\u4f60\u60f3\u5f97\u5230\u7edd\u5bf9\u8def\u5f84\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528?realpath() \u51fd\u6570\uff1a\r\n\r\n$files = glob('..\/images\/a*.jpg');\r\n\r\n\/\/ applies the function to each array element\r\n$files = array_map('realpath',$files);\r\n\r\nprint_r($files);\r\n\/* output looks like:\r\nArray\r\n(\r\n    &#x5B;0] =&gt; C:\\wamp\\www\\images\\apple.jpg\r\n    &#x5B;1] =&gt; C:\\wamp\\www\\images\\art.jpg\r\n)\r\n*\/\r\n<\/pre>\n<p>3. \u5185\u5b58\u4f7f\u7528\u4fe1\u606f<br \/>\n\u89c2\u5bdf\u4f60\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u80fd\u591f\u8ba9\u4f60\u66f4\u597d\u7684\u4f18\u5316\u4f60\u7684\u4ee3\u7801\u3002<\/p>\n<p>PHP \u662f\u6709\u5783\u573e\u56de\u6536\u673a\u5236\u7684\uff0c\u800c\u4e14\u6709\u4e00\u5957\u5f88\u590d\u6742\u7684\u5185\u5b58\u7ba1\u7406\u673a\u5236\u3002\u4f60\u53ef\u4ee5\u77e5\u9053\u4f60\u7684\u811a\u672c\u6240\u4f7f\u7528\u7684\u5185\u5b58\u60c5\u51b5\u3002\u8981\u77e5\u9053\u5f53\u524d\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528?memory_get_usage() \u51fd\u6570\uff0c\u5982\u679c\u4f60\u60f3\u77e5\u9053\u4f7f\u7528\u5185\u5b58\u7684\u5cf0\u503c\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528memory_get_peak_usage() \u51fd\u6570\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\necho &quot;Initial: &quot;.memory_get_usage().&quot; bytes \\n&quot;;\r\n\/* \u8f93\u51fa\r\nInitial: 361400 bytes\r\n*\/\r\n\r\n\/\/ \u4f7f\u7528\u5185\u5b58\r\nfor ($i = 0; $i &lt; 100000; $i++) {\r\n $array &#x5B;]= md5($i);\r\n}\r\n\r\n\/\/ \u5220\u9664\u4e00\u534a\u7684\u5185\u5b58\r\nfor ($i = 0; $i &lt; 100000; $i++) {\r\n unset($array&#x5B;$i]);\r\n}\r\n\r\necho &quot;Final: &quot;.memory_get_usage().&quot; bytes \\n&quot;;\r\n\/* prints\r\nFinal: 885912 bytes\r\n*\/\r\n\r\necho &quot;Peak: &quot;.memory_get_peak_usage().&quot; bytes \\n&quot;;\r\n\/* \u8f93\u51fa\u5cf0\u503c\r\nPeak: 13687072 bytes\r\n*\/\r\n<\/pre>\n<p>4. CPU\u4f7f\u7528\u4fe1\u606f<br \/>\n\u4f7f\u7528?getrusage() \u51fd\u6570\u53ef\u4ee5\u8ba9\u4f60\u77e5\u9053CPU\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u6ce8\u610f\uff0c\u8fd9\u4e2a\u529f\u80fd\u5728Windows\u4e0b\u4e0d\u53ef\u7528\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nprint_r(getrusage());\r\n\/* \u8f93\u51fa\r\nArray\r\n(\r\n    &#x5B;ru_oublock] =&gt; 0\r\n    &#x5B;ru_inblock] =&gt; 0\r\n    &#x5B;ru_msgsnd] =&gt; 2\r\n    &#x5B;ru_msgrcv] =&gt; 3\r\n    &#x5B;ru_maxrss] =&gt; 12692\r\n    &#x5B;ru_ixrss] =&gt; 764\r\n    &#x5B;ru_idrss] =&gt; 3864\r\n    &#x5B;ru_minflt] =&gt; 94\r\n    &#x5B;ru_majflt] =&gt; 0\r\n    &#x5B;ru_nsignals] =&gt; 1\r\n    &#x5B;ru_nvcsw] =&gt; 67\r\n    &#x5B;ru_nivcsw] =&gt; 4\r\n    &#x5B;ru_nswap] =&gt; 0\r\n    &#x5B;ru_utime.tv_usec] =&gt; 0\r\n    &#x5B;ru_utime.tv_sec] =&gt; 0\r\n    &#x5B;ru_stime.tv_usec] =&gt; 6269\r\n    &#x5B;ru_stime.tv_sec] =&gt; 0\r\n)\r\n\r\n*\/\r\n<\/pre>\n<p>\u8fd9\u4e2a\u7ed3\u6784\u770b\u4e0a\u51fa\u5f88\u6666\u6da9\uff0c\u9664\u975e\u4f60\u5bf9CPU\u5f88\u4e86\u89e3\u3002\u4e0b\u9762\u4e00\u4e9b\u89e3\u91ca\uff1a<\/p>\n<p>\u25e6ru_oublock: \u5757\u8f93\u51fa\u64cd\u4f5c<br \/>\n\u25e6ru_inblock: \u5757\u8f93\u5165\u64cd\u4f5c<br \/>\n\u25e6ru_msgsnd: \u53d1\u9001\u7684message<br \/>\n\u25e6ru_msgrcv: \u6536\u5230\u7684message<br \/>\n\u25e6ru_maxrss: \u6700\u5927\u9a7b\u7559\u96c6\u5927\u5c0f<br \/>\n\u25e6ru_ixrss: \u5168\u90e8\u5171\u4eab\u5185\u5b58\u5927\u5c0f<br \/>\n\u25e6ru_idrss:\u5168\u90e8\u975e\u5171\u4eab\u5185\u5b58\u5927\u5c0f<br \/>\n\u25e6ru_minflt: \u9875\u56de\u6536<br \/>\n\u25e6ru_majflt: \u9875\u5931\u6548<br \/>\n\u25e6ru_nsignals: \u6536\u5230\u7684\u4fe1\u53f7<br \/>\n\u25e6ru_nvcsw: \u4e3b\u52a8\u4e0a\u4e0b\u6587\u5207\u6362<br \/>\n\u25e6ru_nivcsw: \u88ab\u52a8\u4e0a\u4e0b\u6587\u5207\u6362<br \/>\n\u25e6ru_nswap: \u4ea4\u6362\u533a<br \/>\n\u25e6ru_utime.tv_usec: \u7528\u6237\u6001\u65f6\u95f4 (microseconds)<br \/>\n\u25e6ru_utime.tv_sec: \u7528\u6237\u6001\u65f6\u95f4(seconds)<br \/>\n\u25e6ru_stime.tv_usec: \u7cfb\u7edf\u5185\u6838\u65f6\u95f4 (microseconds)<br \/>\n\u25e6ru_stime.tv_sec: \u7cfb\u7edf\u5185\u6838\u65f6\u95f4?(seconds)<br \/>\n\u8981\u770b\u5230\u4f60\u7684\u811a\u672c\u6d88\u8017\u4e86\u591a\u5c11CPU\uff0c\u6211\u4eec\u9700\u8981\u770b\u770b\u201c\u7528\u6237\u6001\u7684\u65f6\u95f4\u201d\u548c\u201c\u7cfb\u7edf\u5185\u6838\u65f6\u95f4\u201d\u7684\u503c\u3002\u79d2\u548c\u5fae\u79d2\u90e8\u5206\u662f\u5206\u522b\u63d0\u4f9b\u7684\uff0c\u60a8\u53ef\u4ee5\u628a\u5fae\u79d2\u503c\u9664\u4ee5100\u4e07\uff0c\u5e76\u628a\u5b83\u6dfb\u52a0\u5230\u79d2\u7684\u503c\u540e\uff0c\u53ef\u4ee5\u5f97\u5230\u6709\u5c0f\u6570\u90e8\u5206\u7684\u79d2\u6570\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/\/ sleep for 3 seconds (non-busy)\r\nsleep(3);\r\n\r\n$data = getrusage();\r\necho &quot;User time: &quot;.\r\n ($data&#x5B;'ru_utime.tv_sec'] +\r\n $data&#x5B;'ru_utime.tv_usec'] \/ 1000000);\r\necho &quot;System time: &quot;.\r\n ($data&#x5B;'ru_stime.tv_sec'] +\r\n $data&#x5B;'ru_stime.tv_usec'] \/ 1000000);\r\n\r\n\/* \u8f93\u51fa\r\nUser time: 0.011552\r\nSystem time: 0\r\n*\/\r\nsleep\u662f\u4e0d\u5360\u7528\u7cfb\u7edf\u65f6\u95f4\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u6765\u770b\u4e0b\u9762\u7684\u4e00\u4e2a\u4f8b\u5b50\uff1a\r\n\r\n\/\/ loop 10 million times (busy)\r\nfor($i=0;$i&lt;10000000;$i++) {\r\n\r\n}\r\n\r\n$data = getrusage();\r\necho &quot;User time: &quot;.\r\n ($data&#x5B;'ru_utime.tv_sec'] +\r\n $data&#x5B;'ru_utime.tv_usec'] \/ 1000000);\r\necho &quot;System time: &quot;.\r\n ($data&#x5B;'ru_stime.tv_sec'] +\r\n $data&#x5B;'ru_stime.tv_usec'] \/ 1000000);\r\n\r\n\/* \u8f93\u51fa\r\nUser time: 1.424592\r\nSystem time: 0.004204\r\n*\/\r\n<\/pre>\n<p>\u8fd9\u82b1\u4e86\u5927\u7ea614\u79d2\u7684CPU\u65f6\u95f4\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u90fd\u662f\u7528\u6237\u7684\u65f6\u95f4\uff0c\u56e0\u4e3a\u6ca1\u6709\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<p>\u7cfb\u7edf\u65f6\u95f4\u662fCPU\u82b1\u8d39\u5728\u7cfb\u7edf\u8c03\u7528\u4e0a\u7684\u4e0a\u6267\u884c\u5185\u6838\u6307\u4ee4\u7684\u65f6\u95f4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$start = microtime(true);\r\n\/\/ keep calling microtime for about 3 seconds\r\nwhile(microtime(true) - $start &lt; 3) {\r\n\r\n}\r\n\r\n$data = getrusage();\r\necho &quot;User time: &quot;.\r\n ($data&#x5B;'ru_utime.tv_sec'] +\r\n $data&#x5B;'ru_utime.tv_usec'] \/ 1000000);\r\necho &quot;System time: &quot;.\r\n ($data&#x5B;'ru_stime.tv_sec'] +\r\n $data&#x5B;'ru_stime.tv_usec'] \/ 1000000);\r\n\r\n\/* prints\r\nUser time: 1.088171\r\nSystem time: 1.675315\r\n*\/\r\n<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u66f4\u8017CPU\u3002<\/p>\n<p>5. \u7cfb\u7edf\u5e38\u91cf<br \/>\nPHP \u63d0\u4f9b\u975e\u5e38\u6709\u7528\u7684\u7cfb\u7edf\u5e38\u91cf \u53ef\u4ee5\u8ba9\u4f60\u5f97\u5230\u5f53\u524d\u7684\u884c\u53f7 (__LINE__)\uff0c\u6587\u4ef6 (__FILE__)\uff0c\u76ee\u5f55 (__DIR__)\uff0c\u51fd\u6570\u540d (__FUNCTION__)\uff0c\u7c7b\u540d(__CLASS__)\uff0c\u65b9\u6cd5\u540d(__METHOD__) \u548c\u540d\u5b57\u7a7a\u95f4 (__NAMESPACE__)\uff0c\u5f88\u50cfC\u8bed\u8a00\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4ee5\u4e3a\u8fd9\u4e9b\u4e1c\u897f\u4e3b\u8981\u662f\u7528\u4e8e\u8c03\u8bd5\uff0c\u5f53\u4e5f\u4e0d\u4e00\u5b9a\uff0c\u6bd4\u5982\u6211\u4eec\u53ef\u4ee5\u5728include\u5176\u5b83\u6587\u4ef6\u7684\u65f6\u5019\u4f7f\u7528?__FILE__ (\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728 PHP 5.3\u4ee5\u540e\u4f7f\u7528 __DIR__ )\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ this is relative to the loaded script's path\r\n\/\/ it may cause problems when running scripts from different directories\r\nrequire_once('config\/database.php');\r\n\r\n\/\/ this is always relative to this file's path\r\n\/\/ no matter where it was included from\r\nrequire_once(dirname(__FILE__) . '\/config\/database.php');\r\n<\/pre>\n<p>\u4e0b\u9762\u662f\u4f7f\u7528 __LINE__ \u6765\u8f93\u51fa\u4e00\u4e9bdebug\u7684\u4fe1\u606f\uff0c\u8fd9\u6837\u6709\u52a9\u4e8e\u4f60\u8c03\u8bd5\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ some code\r\n\/\/ ...\r\nmy_debug(&quot;some debug message&quot;, __LINE__);\r\n\/* \u8f93\u51fa\r\nLine 4: some debug message\r\n*\/\r\n\r\n\/\/ some more code\r\n\/\/ ...\r\nmy_debug(&quot;another debug message&quot;, __LINE__);\r\n\/* \u8f93\u51fa\r\nLine 11: another debug message\r\n*\/\r\n\r\nfunction my_debug($msg, $line) {\r\n echo &quot;Line $line: $msg\\n&quot;;\r\n}\r\n<\/pre>\n<p>6.\u751f\u6210\u552f\u4e00\u7684ID<br \/>\n\u6709\u5f88\u591a\u4eba\u4f7f\u7528 md5() \u6765\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684ID\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ generate unique string\r\necho md5(time() . mt_rand(1,1000000));\r\n<\/pre>\n<p>\u5176\u5b9e\uff0cPHP\u4e2d\u6709\u4e00\u4e2a\u53eb?uniqid() \u7684\u51fd\u6570\u662f\u4e13\u95e8\u7528\u6765\u5e72\u8fd9\u4e2a\u7684\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ generate unique string\r\necho uniqid();\r\n\/* \u8f93\u51fa\r\n4bd67c947233e\r\n*\/\r\n\r\n\/\/ generate another unique string\r\necho uniqid();\r\n\/* \u8f93\u51fa\r\n4bd67c9472340\r\n*\/\r\n<\/pre>\n<p>\u53ef\u80fd\u4f60\u4f1a\u6ce8\u610f\u5230\u751f\u6210\u51fa\u6765\u7684ID\u524d\u51e0\u4f4d\u662f\u4e00\u6837\u7684\uff0c\u8fd9\u662f\u56e0\u4e3a\u751f\u6210\u5668\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u7684\u65f6\u95f4\uff0c\u8fd9\u5176\u5b9e\u662f\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684\u529f\u80fd\uff0c\u56e0\u4e3a\u4f60\u662f\u5f88\u5bb9\u6613\u4e3a\u4f60\u7684\u8fd9\u4e9bID\u6392\u5e8f\u7684\u3002\u8fd9\u70b9MD5\u662f\u505a\u4e0d\u5230\u7684\u3002<\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u52a0\u4e0a\u524d\u7f00\u907f\u514d\u91cd\u540d\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ \u524d\u7f00\r\necho uniqid('foo_');\r\n\/* \u8f93\u51fa\r\nfoo_4bd67d6cd8b8f\r\n*\/\r\n\r\n\/\/ \u6709\u66f4\u591a\u7684\u71b5\r\necho uniqid('',true);\r\n\/* \u8f93\u51fa\r\n4bd67d6cd8b926.12135106\r\n*\/\r\n\r\n\/\/ \u90fd\u6709\r\necho uniqid('bar_',true);\r\n\/* \u8f93\u51fa\r\nbar_4bd67da367b650.43684647\r\n*\/\r\n<\/pre>\n<p>\u800c\u4e14\uff0c\u751f\u6210\u51fa\u6765\u7684ID\u4f1a\u6bd4MD5\u751f\u6210\u7684\u8981\u77ed\uff0c\u8fd9\u4f1a\u8ba9\u4f60\u8282\u7701\u5f88\u591a\u7a7a\u95f4\u3002<\/p>\n<p>7. \u5e8f\u5217\u5316<br \/>\n\u4f60\u662f\u5426\u4f1a\u628a\u4e00\u4e2a\u6bd4\u8f83\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u5b58\u5230\u6570\u636e\u5e93\u6216\u662f\u6587\u4ef6\u4e2d\uff1f\u4f60\u5e76\u4e0d\u9700\u8981\u81ea\u5df1\u53bb\u5199\u81ea\u5df1\u7684\u7b97\u6cd5\u3002PHP\u65e9\u5df2\u4e3a\u4f60\u505a\u597d\u4e86\uff0c\u5176\u63d0\u4f9b\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a?serialize() \u548c unserialize():<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ \u4e00\u4e2a\u590d\u6742\u7684\u6570\u7ec4\r\n$myvar = array(\r\n 'hello',\r\n 42,\r\n array(1,'two'),\r\n 'apple'\r\n);\r\n\r\n\/\/ \u5e8f\u5217\u5316\r\n$string = serialize($myvar);\r\n\r\necho $string;\r\n\/* \u8f93\u51fa\r\na:4:{i:0;s:5:&quot;hello&quot;;i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:&quot;two&quot;;}i:3;s:5:&quot;apple&quot;;}\r\n*\/\r\n\r\n\/\/ \u53cd\u5e8f\u4f8b\u5316\r\n$newvar = unserialize($string);\r\n\r\nprint_r($newvar);\r\n\/* \u8f93\u51fa\r\nArray\r\n(\r\n    &#x5B;0] =&gt; hello\r\n    &#x5B;1] =&gt; 42\r\n    &#x5B;2] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; 1\r\n            &#x5B;1] =&gt; two\r\n        )\r\n\r\n    &#x5B;3] =&gt; apple\r\n)\r\n*\/\r\n<\/pre>\n<p>\u8fd9\u662fPHP\u7684\u539f\u751f\u51fd\u6570\uff0c\u7136\u800c\u5728\u4eca\u5929JSON\u8d8a\u6765\u8d8a\u6d41\u884c\uff0c\u6240\u4ee5\u5728PHP5.2\u4ee5\u540e\uff0cPHP\u5f00\u59cb\u652f\u6301JSON\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 json_encode() \u548c json_decode() \u51fd\u6570<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ a complex array\r\n$myvar = array(\r\n 'hello',\r\n 42,\r\n array(1,'two'),\r\n 'apple'\r\n);\r\n\r\n\/\/ convert to a string\r\n$string = json_encode($myvar);\r\n\r\necho $string;\r\n\/* prints\r\n&#x5B;&quot;hello&quot;,42,&#x5B;1,&quot;two&quot;],&quot;apple&quot;]\r\n*\/\r\n\r\n\/\/ you can reproduce the original variable\r\n$newvar = json_decode($string);\r\n\r\nprint_r($newvar);\r\n\/* prints\r\nArray\r\n(\r\n    &#x5B;0] =&gt; hello\r\n    &#x5B;1] =&gt; 42\r\n    &#x5B;2] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; 1\r\n            &#x5B;1] =&gt; two\r\n        )\r\n\r\n    &#x5B;3] =&gt; apple\r\n)\r\n*\/\r\n<\/pre>\n<p>\u8fd9\u770b\u8d77\u6765\u66f4\u4e3a\u7d27\u51d1\u4e00\u4e9b\u4e86\uff0c\u800c\u4e14\u8fd8\u517c\u5bb9\u4e8eJavascript\u548c\u5176\u5b83\u8bed\u8a00\u3002\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e9b\u975e\u5e38\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u6570\u636e\u4e22\u5931\u3002<\/p>\n<p>8. \u5b57\u7b26\u4e32\u538b\u7f29<br \/>\n\u5f53\u6211\u4eec\u8bf4\u5230\u538b\u7f29\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u60f3\u5230\u6587\u4ef6\u538b\u7f29\uff0c\u5176\u5b9e\uff0c\u5b57\u7b26\u4e32\u4e5f\u662f\u53ef\u4ee5\u538b\u7f29\u7684\u3002PHP\u63d0\u4f9b\u4e86?gzcompress() \u548c gzuncompress() \u51fd\u6570\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$string =\r\n&quot;Lorem ipsum dolor sit amet, consectetur\r\nadipiscing elit. Nunc ut elit id mi ultricies\r\nadipiscing. Nulla facilisi. Praesent pulvinar,\r\nsapien vel feugiat vestibulum, nulla dui pretium orci,\r\nnon ultricies elit lacus quis ante. Lorem ipsum dolor\r\nsit amet, consectetur adipiscing elit. Aliquam\r\npretium ullamcorper urna quis iaculis. Etiam ac massa\r\nsed turpis tempor luctus. Curabitur sed nibh eu elit\r\nmollis congue. Praesent ipsum diam, consectetur vitae\r\nornare a, aliquam a nunc. In id magna pellentesque\r\ntellus posuere adipiscing. Sed non mi metus, at lacinia\r\naugue. Sed magna nisi, ornare in mollis in, mollis\r\nsed nunc. Etiam at justo in leo congue mollis.\r\nNullam in neque eget metus hendrerit scelerisque\r\neu non enim. Ut malesuada lacus eu nulla bibendum\r\nid euismod urna sodales. &quot;;\r\n\r\n$compressed = gzcompress($string);\r\n\r\necho &quot;Original size: &quot;. strlen($string).&quot;\\n&quot;;\r\n\/* \u8f93\u51fa\u539f\u59cb\u5927\u5c0f\r\nOriginal size: 800\r\n*\/\r\n\r\necho &quot;Compressed size: &quot;. strlen($compressed).&quot;\\n&quot;;\r\n\/* \u8f93\u51fa\u538b\u7f29\u540e\u7684\u5927\u5c0f\r\nCompressed size: 418\r\n*\/\r\n\r\n\/\/ \u89e3\u538b\u7f29\r\n$original = gzuncompress($compressed);\r\n<\/pre>\n<p>\u51e0\u4e4e\u670950% \u538b\u7f29\u6bd4\u7387\u3002\u540c\u65f6\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528?gzencode() \u548c gzdecode() \u51fd\u6570\u6765\u538b\u7f29\uff0c\u53ea\u4e0d\u7528\u5176\u7528\u4e86\u4e0d\u540c\u7684\u538b\u7f29\u7b97\u6cd5\u3002<\/p>\n<p>9. \u6ce8\u518c\u505c\u6b62\u51fd\u6570<br \/>\n\u6709\u4e00\u4e2a\u51fd\u6570\u53eb\u505a?register_shutdown_function()\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5728\u6574\u4e2a\u811a\u672c\u505c\u65f6\u524d\u8fd0\u884c\u4ee3\u7801\u3002\u8ba9\u6211\u4eec\u770b\u4e0b\u9762\u7684\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ capture the start time\r\n$start_time = microtime(true);\r\n\r\n\/\/ do some stuff\r\n\/\/ ...\r\n\r\n\/\/ display how long the script took\r\necho &quot;execution took: &quot;.\r\n  (microtime(true) - $start_time).\r\n  &quot; seconds.&quot;;\r\n<\/pre>\n<p>\u4e0a\u9762\u8fd9\u4e2a\u793a\u4f8b\u53ea\u4e0d\u8fc7\u662f\u7528\u6765\u8ba1\u7b97\u67d0\u4e2a\u51fd\u6570\u8fd0\u884c\u7684\u65f6\u95f4\u3002\u7136\u540e\uff0c\u5982\u679c\u4f60\u5728\u51fd\u6570\u4e2d\u95f4\u8c03\u7528?exit() \u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u7684\u6700\u540e\u7684\u4ee3\u7801\u5c06\u4e0d\u4f1a\u88ab\u8fd0\u884c\u5230\u3002\u5e76\u4e14\uff0c\u5982\u679c\u8be5\u811a\u672c\u5728\u6d4f\u89c8\u5668\u7ec8\u6b62\uff08\u7528\u6237\u6309\u505c\u6b62\u6309\u94ae\uff09\uff0c\u5176\u4e5f\u65e0\u6cd5\u88ab\u8fd0\u884c\u3002<\/p>\n<p>\u800c\u5f53\u6211\u4eec\u4f7f\u7528\u4e86register_shutdown_function()\u540e\uff0c\u4f60\u7684\u7a0b\u5e8f\u5c31\u7b97\u662f\u5728\u811a\u672c\u88ab\u505c\u6b62\u540e\u4e5f\u4f1a\u88ab\u8fd0\u884c\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$start_time = microtime(true);\r\n\r\nregister_shutdown_function('my_shutdown');\r\n\r\n\/\/ do some stuff\r\n\/\/ ...\r\n\r\nfunction my_shutdown() {\r\n global $start_time;\r\n\r\n echo &quot;execution took: &quot;.\r\n   (microtime(true) - $start_time).\r\n   &quot; seconds.&quot;;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4e5d\u4e2aPHP\u4e2d\u5f88\u6709\u7528\u7684\u529f\u80fd\uff0c\u4e0d\u77e5\u9053\u4f60\u7528\u8fc7\u4e86\u5417\uff1f 1. \u51fd\u6570\u7684\u4efb\u610f\u6570\u76ee\u7684\u53c2\u6570 \u4f60\u53ef\u80fd\u77e5\u9053PHP\u5141\u8bb8\u4f60\u5b9a\u4e49\u4e00\u4e2a [&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-1070","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1070","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=1070"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1070\/revisions"}],"predecessor-version":[{"id":5230,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1070\/revisions\/5230"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}