[导读] 做过PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以帮助你在做PHP开发时更加得心应手,本文将分享8个开发必备的PHP功能,个个都非常实用,希望各位PHP开发者能够掌握。1、传递任意数量的函数

01.// 两个默认参数的函数 02.function foo($arg1 = ”, $arg2 = ”) { 03.echo “arg1: $arg1\n”; 04.echo “arg2: $arg2\n”; 05.} 06.foo(‘hello’,‘world’); 07./* 输出:08.arg1: hello09.arg2: world10.*/ 11.foo(); 12./* 输出:13.arg1:14.arg2:15.*/ 16.下面这个示例是PHP的不定参数用法,其使用到了 func_get_args()方法: 17.// 是的,形参列表为空 18.function foo() { 19.// 取得所有的传入参数的数组 20.$args = func_get_args(); 21.foreach ($args as $k => $v) { 22.echo “arg”.($k+1).”: $v\n”; 23.} 24.} 25.foo(); 26./* 什么也不会输出 */ 27.foo(‘hello’); 28./* 输出29.arg1: hello30.*/ 31.foo(‘hello’, ‘world’, ‘again’); 32./* 输出33.arg1: hello34.arg2: world35.arg3: again36.*/01.// 取得所有的后缀为PHP的文件 02.$files = glob(‘*.php’); 03.print_r($files); 04./* 输出:05.Array06.(07.[0] => phptest.php08.[1] => pi.php09.[2] => post_output.php10.[3] => test.php11.)12.*/01.// 取PHP文件和TXT文件 02.$files = glob(‘*.{php,txt}’, GLOB_BRACE); 03.print_r($files); 04./* 输出:05.Array06.(07.[0] => phptest.php08.[1] => pi.php09.[2] => post_output.php10.[3] => test.php11.[4] => log.txt12.[5] => test.txt13.)14.*/01.$files = glob(‘../images/a*.jpg’); 02.print_r($files); 03./* 输出:04.Array05.(06.[0] => ../images/apple.jpg07.[1] => ../images/art.jpg08.)09.*/01.$files = glob(‘../images/a*.jpg’); 02.// applies the function to each array element 03.$files = array_map(‘realpath’,$files); 04.print_r($files); 05./* output looks like:06.Array07.(08.[0] => C:\wamp\www\images\apple.jpg09.[1] => C:\wamp\www\images\art.jpg10.)11.*/01.echo “Initial: “.memory_get_usage().” bytes \n”; 02./* 输出03.Initial: 361400 bytes04.*/ 05.// 使用内存 06.for ($i = 0; $i < 100000; $i++) { 07.$array []= md5($i); 08.} 09.// 删除一半的内存 10.for ($i = 0; $i < 100000; $i++) { 11.unset($array[$i]); 12.} 13.echo “Final: “.memory_get_usage().” bytes \n”; 14./* prints15.Final: 885912 bytes16.*/ 17.echo “Peak: “.memory_get_peak_usage().” bytes \n”; 18./* 输出峰值19.Peak: 13687072 bytes20.*/01.print_r(getrusage()); 02./* 输出03.Array04.(05.[ru_oublock] => 006.[ru_inblock] => 007.[ru_msgsnd] => 208.[ru_msgrcv] => 309.[ru_maxrss] => 1269210.[ru_ixrss] => 76411.[ru_idrss] => 386412.[ru_minflt] => 9413.[ru_majflt] => 014.[ru_nsignals] => 115.[ru_nvcsw] => 6716.[ru_nivcsw] => 417.[ru_nswap] => 018.[ru_utime.tv_usec] => 019.[ru_utime.tv_sec] => 020.[ru_stime.tv_usec] => 626921.[ru_stime.tv_sec] => 022.)23.*/01.// sleep for 3 seconds (non-busy) 02.sleep(3); 03.$data = getrusage(); 04.echo “User time: “. 05.($data[‘ru_utime.tv_sec‘] + 06.$data[‘ru_utime.tv_usec‘] / 1000000); 07.echo “System time: “. 08.($data[‘ru_stime.tv_sec‘] + 09.$data[‘ru_stime.tv_usec‘] / 1000000); 10./* 输出11.User time: 0.01155212.System time: 013.*/01.// loop 10 million times (busy) 02.for($i=0;$i<10000000;$i++) { 03.} 04.$data = getrusage(); 05.echo “User time: “. 06.($data[‘ru_utime.tv_sec‘] + 07.$data[‘ru_utime.tv_usec‘] / 1000000); 08.echo “System time: “. 09.($data[‘ru_stime.tv_sec‘] + 10.$data[‘ru_stime.tv_usec‘] / 1000000); 11./* 输出12.User time: 1.42459213.System time: 0.00420414.*/01.$start = microtime(true); 02.// keep calling microtime for about 3 seconds 03.while(microtime(true) – $start < 3) { 04.} 05.$data = getrusage(); 06.echo “User time: “. 07.($data[‘ru_utime.tv_sec‘] + 08.$data[‘ru_utime.tv_usec‘] / 1000000); 09.echo “System time: “. 10.($data[‘ru_stime.tv_sec‘] + 11.$data[‘ru_stime.tv_usec‘] / 1000000); 12./* prints13.User time: 1.08817114.System time: 1.67531515.*/1.// this is relative to the loaded script’s path 2.// it may cause problems when running scripts from different directories 3.require_once(‘config/database.php’); 4.// this is always relative to this file’s path 5.// no matter where it was included from 6.require_once(dirname(__FILE__) . ‘/config/database.php’);01.// some code 02.// … 03.my_debug(“some debug message”, __LINE__); 04./* 输出05.Line 4: some debug message06.*/ 07.// some more code 08.// … 09.my_debug(“another debug message”, __LINE__); 10./* 输出11.Line 11: another debug message12.*/ 13.function my_debug($msg, $line) { 14.echo “Line $line: $msg\n”; 15.}01.// generate unique string 02.echo uniqid(); 03./* 输出04.4bd67c947233e05.*/ 06.// generate another unique string 07.echo uniqid(); 08./* 输出09.4bd67c947234010.*/01.// 前缀 02.echo uniqid(‘foo_’); 03./* 输出04.foo_4bd67d6cd8b8f05.*/ 06.// 有更多的熵 07.echo uniqid(”,true); 08./* 输出09.4bd67d6cd8b926.1213510610.*/ 11.// 都有 12.echo uniqid(‘bar_’,true); 13./* 输出14.bar_4bd67da367b650.4368464715.*/01.// 一个复杂的数组 02.$myvar = array( 03.‘hello’, 04.42, 05.array(1,’two’), 06.‘apple’ 07.); 08.// 序列化 09.$string = serialize($myvar); 10.echo $string; 11./* 输出12.a:4:{i:0;s:5:”hello”;i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:”two”;}i:3;s:5:”apple”;}13.*/ 14.// 反序例化 15.$newvar = unserialize($string); 16.print_r($newvar); 17./* 输出18.Array19.(20.[0] => hello21.[1] => 4222.[2] => Array23.(24.[0] => 125.[1] => two26.)27.[3] => apple28.)29.*/01.// a complex array 02.$myvar = array( 03.‘hello’, 04.42, 05.array(1,’two’), 06.‘apple’ 07.); 08.// convert to a string 09.$string = json_encode($myvar); 10.echo $string; 11./* prints12.["hello",42,[1,"two"],”apple”]13.*/ 14.// you can reproduce the original variable 15.$newvar = json_decode($string); 16.print_r($newvar); 17./* prints18.Array19.(20.[0] => hello21.[1] => 4222.[2] => Array23.(24.[0] => 125.[1] => two26.)27.[3] => apple28.)29.*/01.$string = 02.“Lorem ipsum dolor sit amet, consectetur 03.adipiscing elit. Nunc ut elit id mi ultricies 04.adipiscing. Nulla facilisi. Praesent pulvinar, 05.sapien vel feugiat vestibulum, nulla dui pretium orci, 06.non ultricies elit lacus quis ante. Lorem ipsum dolor 07.sit amet, consectetur adipiscing elit. Aliquam 08.pretium ullamcorper urna quis iaculis. Etiam ac massa 09.sed turpis tempor luctus. Curabitur sed nibh eu elit 10.mollis congue. Praesent ipsum diam, consectetur vitae 11.ornare a, aliquam a nunc. In id magna pellentesque 12.tellus posuere adipiscing. Sed non mi metus, at lacinia 13.augue. Sed magna nisi, ornare in mollis in, mollis 14.sed nunc. Etiam at justo in leo congue mollis. 15.Nullam in neque eget metus hendrerit scelerisque 16.eu non enim. Ut malesuada lacus eu nulla bibendum 17.id euismod urna sodales. “; 18.$compressed = gzcompress($string); 19.echo “Original size: “. strlen($string).”\n”; 20./* 输出原始大小21.Original size: 80022.*/ 23.echo “Compressed size: “. strlen($compressed).”\n”; 24./* 输出压缩后的大小25.Compressed size: 41826.*/ 27.// 解压缩 28.$original = gzuncompress($compressed);
原文:http://www.cnblogs.com/u0mo5/p/4299934.html