九个PHP中很有用的功能

下面是九个PHP中很有用的功能,不知道你用过了吗? 1. 函数的任意数目的参数 你可能知道PHP允许你定义一个默认参数的函数。但你可能并不知道PHP还允许你定义一个完全任意的参数的函数 下面是一个示例向你展示了默认参数的函数: 查看源代码 打印帮助 01 // 两个默认参数的函数 02 function foo($arg1 = '', $arg2 = '') { 03 04 echo "arg1: $arg1/n"; 05 echo "arg2: $arg2/n"; 06 07 } 08 09 foo('hello','world'); 10 /* 输出: 11 arg1: hello 12 arg2: world 13 */ 14 15 foo(); 16 /* 输出: 17 arg1: 18 arg2: 19 */ 现在我们来看一看一个不定参数的函数,其使用到了?func_get_args()方法: 查看源代码 打印帮助 01 // 是的,形参列表为空 02 function foo() { 03 04 // 取得所有的传入参数的数组 05 $args = func_get_args(); 06 07 foreach ($args as $k => $v) { 08 echo "arg".($k+1).": $v/n"; 09 } 10 11 } 12 13 foo(); 14 /* 什么也不会输出 */ 15 16 foo('hello'); 17 /* 输出 18 arg1: hello 19 */ 20 21 foo('hello', 'world', 'again'); 22 /* 输出 23 arg1: hello 24 arg2: world 25 arg3: again 26 */ 2. 使用 Glob() 查找文件 很多PHP的函数都有一个比较长的自解释的函数名,但是,当你看到?glob() 的时候,你可能并不知道这个函数是用来干什么的,除非你对它已经很熟悉了。 你可以认为这个函数就好?scandir() 一样,其可以用来查找文件。 查看源代码 打印帮助 01 // 取得所有的后缀为PHP的文件 02 $files = glob('*.php'); 03 04 print_r($files); 05 /* 输出: 06 Array 07 ( 08 [0] => phptest.php 09 [1] => pi.php 10 [2] => post_output.php 11 [3] => test.php 12 ) 13 */ 你还可以查找多种后缀名 查看源代码 打印帮助 01 // 取PHP文件和TXT文件 02 $files = glob('*.{php,txt}', GLOB_BRACE); 03 04 print_r($files); 05 /* 输出: 06 Array 07 ( 08 [0] => phptest.php 09 [1] => pi.php 10 [2] => post_output.php 11 [3] => test.php 12 [4] => log.txt 13 [5] => test.txt 14 ) 15 */ 你还可以加上路径: 查看源代码 打印帮助 01 $files = glob('../images/a*.jpg'); 02 03 print_r($files); 04 /* 输出: 05 Array 06 ( 07 [0] => ../images/apple.jpg 08 [1] => ../images/art.jpg 09 ) 10 */ 如果你想得到绝对路径,你可以调用?realpath() 函数: 查看源代码 打印帮助 01 $files = glob('../images/a*.jpg'); 02 03 // applies the function to each array element 04 $files = array_map('realpath',$files); 05 06 print_r($files); 07 /* output looks like: 08 Array 09 ( 10 [0] => C:/wamp/www/images/apple.jpg 11 [1] => C:/wamp/www/images/art.jpg 12 ) 13 */ 3. 内存使用信息 观察你程序的内存使用能够让你更好的优化你的代码。 PHP 是有垃圾回收机制的,而且有一套很复杂的内存管理机制。你可以知道你的脚本所使用的内存情况。要知道当前内存使用情况,你可以使用?memory_get_usage() 函数,如果你想知道使用内存的峰值,你可以调用memory_get_peak_usage() 函数。 查看源代码 打印帮助 01 echo "Initial: ".memory_get_usage()." bytes /n"; 02 /* 输出 03 Initial: 361400 bytes 04 */ 05 06 // 使用内存 07 for ($i = 0; $i < 100000; $i++) { 08 $array []= md5($i); 09 } 10 11 // 删除一半的内存 12 for ($i = 0; $i < 100000; $i++) { 13 unset($array[$i]); 14 } 15 16 echo "Final: ".memory_get_usage()." bytes /n"; 17 /* prints 18 Final: 885912 bytes 19 */ 20 21 echo "Peak: ".memory_get_peak_usage()." bytes /n"; 22 /* 输出峰值 23 Peak: 13687072 bytes 24 */ 4. CPU使用信息 使用?getrusage() 函数可以让你知道CPU的使用情况。注意,这个功能在Windows下不可用。 查看源代码 打印帮助 01 print_r(getrusage()); 02 /* 输出 03 Array 04 ( 05 [ru_oublock] => 0 06 [ru_inblock] => 0 07 [ru_msgsnd] => 2 08 [ru_msgrcv] => 3 09 [ru_maxrss] => 12692 10 [ru_ixrss] => 764 11 [ru_idrss] => 3864 12 [ru_minflt] => 94 13 [ru_majflt] => 0 14 [ru_nsignals] => 1 15 [ru_nvcsw] => 67 16 [ru_nivcsw] => 4 17 [ru_nswap] => 0 18 [ru_utime.tv_usec] => 0 19 [ru_utime.tv_sec] => 0 20 [ru_stime.tv_usec] => 6269 21 [ru_stime.tv_sec] => 0 22 ) 23 24 */ 这个结构看上出很晦涩,除非你对CPU很了解。下面一些解释: * ru_oublock: 块输出操作 * ru_inblock: 块输入操作 * ru_msgsnd: 发送的message * ru_msgrcv: 收到的message * ru_maxrss: 最大驻留集大小 * ru_ixrss: 全部共享内存大小 * ru_idrss:全部非共享内存大小 * ru_minflt: 页回收 * ru_majflt: 页失效 * ru_nsignals: 收到的信号 * ru_nvcsw: 主动上下文切换 * ru_nivcsw: 被动上下文切换 * ru_nswap: 交换区 * ru_utime.tv_usec: 用户态时间 (microseconds) * ru_utime.tv_sec: 用户态时间(seconds) * ru_stime.tv_usec: 系统内核时间 (microseconds) * ru_stime.tv_sec: 系统内核时间?(seconds) 要看到你的脚本消耗了多少CPU,我们需要看看“用户态的时间”和“系统内核时间”的值。秒和微秒部分是分别提供的,您可以把微秒值除以100万,并把它添加到秒的值后,可以得到有小数部分的秒数。 查看源代码 打印帮助 01 // sleep for 3 seconds (non-busy) 02 sleep(3); 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 /* 输出 13 User time: 0.011552 14 System time: 0 15 */ sleep是不占用系统时间的,我们可以来看下面的一个例子: 查看源代码 打印帮助 01 // loop 10 million times (busy) 02 for($i=0;$i<10000000;$i++) { 03 04 } 05 06 $data = getrusage(); 07 echo "User time: ". 08 ($data['ru_utime.tv_sec'] + 09 $data['ru_utime.tv_usec'] / 1000000); 10 echo "System time: ". 11 ($data['ru_stime.tv_sec'] + 12 $data['ru_stime.tv_usec'] / 1000000); 13 14 /* 输出 15 User time: 1.424592 16 System time: 0.004204 17 */ 这花了大约14秒的CPU时间,几乎所有的都是用户的时间,因为没有系统调用。 系统时间是CPU花费在系统调用上的上执行内核指令的时间。下面是一个例子: 查看源代码 打印帮助 01 $start = microtime(true); 02 // keep calling microtime for about 3 seconds 03 while(microtime(true) - $start < 3) { 04 05 } 06 07 $data = getrusage(); 08 echo "User time: ". 09 ($data['ru_utime.tv_sec'] + 10 $data['ru_utime.tv_usec'] / 1000000); 11 echo "System time: ". 12 ($data['ru_stime.tv_sec'] + 13 $data['ru_stime.tv_usec'] / 1000000); 14 15 /* prints 16 User time: 1.088171 17 System time: 1.675315 18 */ 我们可以看到上面这个例子更耗CPU。 5. 系统常量 PHP 提供非常有用的系统常量 可以让你得到当前的行号 (__LINE__),文件 (__FILE__),目录 (__DIR__),函数名 (__FUNCTION__),类名(__CLASS__),方法名(__METHOD__) 和名字空间 (__NAMESPACE__),很像C语言。 我们可以以为这些东西主要是用于调试,当也不一定,比如我们可以在include其它文件的时候使用?__FILE__ (当然,你也可以在 PHP 5.3以后使用 __DIR__ ),下面是一个例子。 查看源代码 打印帮助 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 5 // this is always relative to this file's path 6 // no matter where it was included from 7 require_once(dirname(__FILE__) . '/config/database.php'); 下面是使用 __LINE__ 来输出一些debug的信息,这样有助于你调试程序: 查看源代码 打印帮助 01 // some code 02 // ... 03 my_debug("some debug message", __LINE__); 04 /* 输出 05 Line 4: some debug message 06 */ 07 08 // some more code 09 // ... 10 my_debug("another debug message", __LINE__); 11 /* 输出 12 Line 11: another debug message 13 */ 14 15 function my_debug($msg, $line) { 16 echo "Line $line: $msg/n"; 17 } 6.生成唯一的ID 有很多人使用 md5() 来生成一个唯一的ID,如下所示: 查看源代码 打印帮助 1 // generate unique string 2 echo md5(time() . mt_rand(1,1000000)); 其实,PHP中有一个叫?uniqid() 的函数是专门用来干这个的: 查看源代码 打印帮助 01 // generate unique string 02 echo uniqid(); 03 /* 输出 04 4bd67c947233e 05 */ 06 07 // generate another unique string 08 echo uniqid(); 09 /* 输出 10 4bd67c9472340 11 */ 可能你会注意到生成出来的ID前几位是一样的,这是因为生成器依赖于系统的时间,这其实是一个非常不错的功能,因为你是很容易为你的这些ID排序的。这点MD5是做不到的。 你还可以加上前缀避免重名: 查看源代码 打印帮助 01 // 前缀 02 echo uniqid('foo_'); 03 /* 输出 04 foo_4bd67d6cd8b8f 05 */ 06 07 // 有更多的熵 08 echo uniqid('',true); 09 /* 输出 10 4bd67d6cd8b926.12135106 11 */ 12 13 // 都有 14 echo uniqid('bar_',true); 15 /* 输出 16 bar_4bd67da367b650.43684647 17 */ 而且,生成出来的ID会比MD5生成的要短,这会让你节省很多空间。 7. 序列化 你是否会把一个比较复杂的数据结构存到数据库或是文件中?你并不需要自己去写自己的算法。PHP早已为你做好了,其提供了两个函数:?serialize() 和 unserialize(): 查看源代码 打印帮助 01 // 一个复杂的数组 02 $myvar = array( 03 'hello', 04 42, 05 array(1,'two'), 06 'apple' 07 ); 08 09 // 序列化 10 $string = serialize($myvar); 11 12 echo $string; 13 /* 输出 14 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";} 15 */ 16 17 // 反序例化 18 $newvar = unserialize($string); 19 20 print_r($newvar); 21 /* 输出 22 Array 23 ( 24 [0] => hello 25 [1] => 42 26 [2] => Array 27 ( 28 [0] => 1 29 [1] => two 30 ) 31 32 [3] => apple 33 ) 34 */ 这是PHP的原生函数,然而在今天JSON越来越流行,所以在PHP5.2以后,PHP开始支持JSON,你可以使用 json_encode() 和 json_decode() 函数 查看源代码 打印帮助 01 // a complex array 02 $myvar = array( 03 'hello', 04 42, 05 array(1,'two'), 06 'apple' 07 ); 08 09 // convert to a string 10 $string = json_encode($myvar); 11 12 echo $string; 13 /* prints 14 ["hello",42,[1,"two"],"apple"] 15 */ 16 17 // you can reproduce the original variable 18 $newvar = json_decode($string); 19 20 print_r($newvar); 21 /* prints 22 Array 23 ( 24 [0] => hello 25 [1] => 42 26 [2] => Array 27 ( 28 [0] => 1 29 [1] => two 30 ) 31 32 [3] => apple 33 ) 34 */ 这看起来更为紧凑一些了,而且还兼容于Javascript和其它语言。但是对于一些非常复杂的数据结构,可能会造成数据丢失。 8. 字符串压缩 当我们说到压缩,我们可能会想到文件压缩,其实,字符串也是可以压缩的。PHP提供了?gzcompress() 和 gzuncompress() 函数: 查看源代码 打印帮助 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 19 $compressed = gzcompress($string); 20 21 echo "Original size: ". strlen($string)."/n"; 22 /* 输出原始大小 23 Original size: 800 24 */ 25 26 echo "Compressed size: ". strlen($compressed)."/n"; 27 /* 输出压缩后的大小 28 Compressed size: 418 29 */ 30 31 // 解压缩 32 $original = gzuncompress($compressed); 几乎有50% 压缩比率。同时,你还可以使用?gzencode() 和 gzdecode() 函数来压缩,只不用其用了不同的压缩算法。 9. 注册停止函数 有一个函数叫做?register_shutdown_function(),可以让你在整个脚本停时前运行代码。让我们看下面的一个示例: 查看源代码 打印帮助 01 // capture the start time 02 $start_time = microtime(true); 03 04 // do some stuff 05 // ... 06 07 // display how long the script took 08 echo "execution took: ". 09 (microtime(true) - $start_time). 10 " seconds."; 上面这个示例只不过是用来计算某个函数运行的时间。然后,如果你在函数中间调用?exit() 函数,那么你的最后的代码将不会被运行到。并且,如果该脚本在浏览器终止(用户按停止按钮),其也无法被运行。 而当我们使用了register_shutdown_function()后,你的程序就算是在脚本被停止后也会被运行: 查看源代码 打印帮助 01 $start_time = microtime(true); 02 03 register_shutdown_function('my_shutdown'); 04 05 // do some stuff 06 // ... 07 08 function my_shutdown() { 09 global $start_time; 10 11 echo "execution took: ". 12 (microtime(true) - $start_time). 13 " seconds."; 14 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值