8个必备的PHP功能开发(2)

// sleep for 3 seconds (non-busy) sleep(3); $data = getrusage(); echo “User time: “. ($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000); echo “System time: “. ($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000); /* 输出 User time: 0.011552 System time: 0 */ sleep是不占用系统时间的,我们可以来看下面的一个例子: // loop 10 million times (busy) for($i=0;$i<10000000;$i++) { } $data = getrusage(); echo “User time: “. ($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000); echo “System time: “. ($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000); /* 输出 User time: 1.424592 System time: 0.004204 */

这花了大约14秒的CPU时间,几乎所有的都是用户的时间,因为没有系统调用。
系统时间是CPU花费在系统调用上的上执行内核指令的时间。下面是一个例子:

$start = microtime(true); // keep calling microtime for about 3 seconds while(microtime(true) – $start < 3) { } $data = getrusage(); echo “User time: “. ($data['ru_utime.tv_sec'] + $data['ru_utime.tv_usec'] / 1000000); echo “System time: “. ($data['ru_stime.tv_sec'] + $data['ru_stime.tv_usec'] / 1000000); /* prints User time: 1.088171 System time: 1.675315 */

我们可以看到上面这个例子更耗CPU。 
5、获取系统常量 
 PHP 提供非常有用的系统常量 可以让你得到当前的行号 (__LINE__),文件 (__FILE__),目录 (__DIR__),函数名 (__FUNCTION__),类名(__CLASS__),方法名(__METHOD__) 和名字空间 (__NAMESPACE__),很像C语言。  
我们可以以为这些东西主要是用于调试,当也不一定,比如我们可以在include其它文件的时候使用?__FILE__ (当然,你也可以在 PHP 5.3以后使用 __DIR__ ),下面是一个例子。

// this is relative to the loaded script's path // it may cause problems when running scripts from different directories require_once(‘config/database.php'); // this is always relative to this file's path // no matter where it was included from require_once(dirname(__FILE__) . ‘/config/database.php');

下面是使用 __LINE__ 来输出一些debug的信息,这样有助于你调试程序:

// some code // … my_debug(“some debug message”, __LINE__); /* 输出 Line 4: some debug message */ // some more code // … my_debug(“another debug message”, __LINE__); /* 输出 Line 11: another debug message */ function my_debug($msg, $line) { echo “Line $line: $msg\n”; }

6、生成唯一的id 
 很多朋友都利用md5()来生成唯一的编号,但是md5()有几个缺点:1、无序,导致数据库中排序性能下降。2、太长,需要更多的存储空间。其实PHP中自带一个函数来生成唯一的id,这个函数就是uniqid()。下面是用法:

// generate unique string echo uniqid(); /* 输出 4bd67c947233e */ // generate another unique string echo uniqid(); /* 输出 4bd67c9472340 */

该算法是根据CPU时间戳来生成的,所以在相近的时间段内,id前几位是一样的,这也方便id的排序,如果你想更好的避免重复,可以在id前加上前缀,如:

// 前缀 echo uniqid(‘foo_'); /* 输出 foo_4bd67d6cd8b8f */ // 有更多的熵 echo uniqid(”,true); /* 输出 4bd67d6cd8b926.12135106 */ // 都有 echo uniqid(‘bar_',true); /* 输出 bar_4bd67da367b650.43684647 */

7、序列化 
PHP序列化功能大家可能用的比较多,也比较常见,当你需要把数据存到数据库或者文件中是,你可以利用PHP中的serialize() 和 unserialize()方法来实现序列化和反序列化,代码如下:

// 一个复杂的数组 $myvar = array( ‘hello', 42, array(1,'two'), ‘apple' ); // 序列化 $string = serialize($myvar); echo $string; /* 输出 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”;} */ // 反序例化 $newvar = unserialize($string); print_r($newvar); /* 输出 Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */

如何序列化成json格式呢,放心,php也已经为你做好了,使用php 5.2以上版本的用户可以使用json_encode() 和 json_decode() 函数来实现json格式的序列化,代码如下:

// a complex array $myvar = array( ‘hello', 42, array(1,'two'), ‘apple' ); // convert to a string $string = json_encode($myvar); echo $string; /* prints ["hello",42,[1,"two"],”apple”] */ // you can reproduce the original variable $newvar = json_decode($string); print_r($newvar); /* prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1417d768623c908e61477f6bdc6e172a.html