即使使用 PHP 多年,也會(huì)偶然發(fā)現(xiàn)一些未曾了解的函數(shù)和功能。其中有些是非常有用的,但沒有得到充分利用。并不是所有人都會(huì)從頭到尾一頁一頁地閱讀手冊和函數(shù)參考!1、任意參數(shù)數(shù)目的函數(shù)你可能已經(jīng)知道,PHP 允許定義可選參數(shù)的函數(shù)。但也有完全允許任意數(shù)目的函數(shù)參數(shù)的方法。以下是可選參數(shù)的例子:// function with 2 optional argumentsfunction foo($arg1 = '', $arg2 = '') { echo arg1: $arg1/n echo arg2: $arg2/n foo('hello','world');/* prints:arg1: helloarg2: worldfoo();/* prints:arg1:arg2:現(xiàn)在讓我們看看如何建立能夠接受任何參數(shù)數(shù)目的函數(shù)。這一次需要使用 func_get_args() 函數(shù):// yes, the argument list can be emptyfunction foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k = $v) { echo arg .($k+1). : $v/n foo();/* prints nothing */foo('hello');/* printsarg1: hellofoo('hello', 'world', 'again');/* printsarg1: helloarg2: worldarg3: again2、使用 Glob() 查找文件許多 PHP 函數(shù)具有長描述性的名稱。然而可能會(huì)很難說出 glob() 函數(shù)能做的事情,除非你已經(jīng)通過多次使用并熟悉了它。可以把它看作是比 scandir() 函數(shù)更強(qiáng)大的版本,可以按照某種模式搜索文件。// get all php files$files = glob('*.php');print_r($files);/* output looks like:Array [0] = phptest.php [1] = pi.php [2] = post_output.php [3] = test.php你可以像這樣獲得多個(gè)文件:// get all php files AND txt files$files = glob('*.{php,txt}', GLOB_BRACE);print_r($files);/* output looks like:Array [0] = phptest.php [1] = pi.php [2] = post_output.php [3] = test.php [4] = log.txt [5] = test.txt請注意,這些文件其實(shí)是可以返回一個(gè)路徑,這取決于查詢條件:$files = glob('../images/a*.jpg');print_r($files);/* output looks like:Array [0] = ../images/apple.jpg [1] = ../images/art.jpg如果你想獲得每個(gè)文件的完整路徑,你可以調(diào)用 realpath() 函數(shù): $files = glob('../images/a*.jpg');// applies the function to each array element$files = array_map('realpath',$files);print_r($files);/* output looks like:Array [0] = C:/wamp/www/images/apple.jpg [1] = C:/wamp/www/images/art.jpg3、內(nèi)存使用信息通過偵測腳本的內(nèi)存使用情況,有利于代碼的優(yōu)化。PHP 提供了一個(gè)垃圾收集器和一個(gè)非常復(fù)雜的內(nèi)存管理器。腳本執(zhí)行時(shí)所使用的內(nèi)存量,有升有跌。為了得到當(dāng)前的內(nèi)存使用情況,我們可以使用 memory_get_usage() 函數(shù)。如果需要獲得任意時(shí)間點(diǎn)的最高內(nèi)存使用量,則可以使用 memory_limit() 函數(shù)。echo Initial: .memory_get_usage(). bytes /n /* printsInitial: 361400 bytes// let's use up some memoryfor ($i = 0; $i 100000; $i++) { $array []= md5($i);// let's remove half of the arrayfor ($i = 0; $i 100000; $i++) { unset($array[$i]);echo Final: .memory_get_usage(). bytes /n /* printsFinal: 885912 bytesecho Peak: .memory_get_peak_usage(). bytes /n /* printsPeak: 13687072 bytes*/4、CPU 使用信息為此,我們要利用 getrusage() 函數(shù)。請記住這個(gè)函數(shù)不適用于 Windows 平臺(tái)。print_r(getrusage());/* printsArray [ru_oublock] = 0 [ru_inblock] = 0 [ru_msgsnd] = 2 [ru_msgrcv] = 3 [ru_maxrss] = 12692 [ru_ixrss] = 764 [ru_idrss] = 3864 [ru_minflt] = 94 [ru_majflt] = 0 [ru_nsignals] = 1 [ru_nvcsw] = 67 [ru_nivcsw] = 4 [ru_nswap] = 0 [ru_utime.tv_usec] = 0 [ru_utime.tv_sec] = 0 [ru_stime.tv_usec] = 6269 [ru_stime.tv_sec] = 0*/這可能看起來有點(diǎn)神秘,除非你已經(jīng)有系統(tǒng)管理員權(quán)限。以下是每個(gè)值的具體說明(你不需要記住這些):ru_oublock: block output operationsru_inblock: block input operationsru_msgsnd: messages sentru_msgrcv: messages receivedru_maxrss: maximum resident set sizeru_ixrss: integral shared memory sizeru_idrss: integral unshared data sizeru_minflt: page reclaimsru_majflt: page faultsru_nsignals: signals receivedru_nvcsw: voluntary context switchesru_nivcsw: involuntary context switchesru_nswap: swapsru_utime.tv_usec: user time used (microseconds)ru_utime.tv_sec: user time used (seconds)ru_stime.tv_usec: system time used (microseconds)ru_stime.tv_sec: system time used (seconds)要知道腳本消耗多少 CPU 功率,我們需要看看 user time 和 system time 兩個(gè)參數(shù)的值。秒和微秒部分默認(rèn)是單獨(dú)提供的。你可以除以 100 萬微秒,并加上秒的參數(shù)值,得到一個(gè)十進(jìn)制的總秒數(shù)。讓我們來看一個(gè)例子:// 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);/* printsUser time: 0.011552System time: 0盡管腳本運(yùn)行用了大約 3 秒鐘,CPU 使用率卻非常非常低。因?yàn)樵谒哌\(yùn)行的過程中,該腳本實(shí)際上不消耗 CPU 資源。還有許多其他的任務(wù),可能需要一段時(shí)間,但不占用類似等待磁盤操作等 CPU 時(shí)間。因此正如你所看到的,CPU 使用率和運(yùn)行時(shí)間的實(shí)際長度并不總是相同的。下面是一個(gè)例子:// 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);/* printsUser time: 1.424592System time: 0.004204這花了大約 1.4 秒的 CPU 時(shí)間,但幾乎都是用戶時(shí)間,因?yàn)闆]有系統(tǒng)調(diào)用。系統(tǒng)時(shí)間是指花費(fèi)在執(zhí)行程序的系統(tǒng)調(diào)用時(shí)的 CPU 開銷。下面是一個(gè)例子:$start = microtime(true);// keep calling microtime for about 3 secondswhile(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);/* printsUser time: 1.088171System time: 1.675315現(xiàn)在我們有相當(dāng)多的系統(tǒng)時(shí)間占用。這是因?yàn)槟_本多次調(diào)用 microtime() 函數(shù),該函數(shù)需要向操作系統(tǒng)發(fā)出請求,以獲取所需時(shí)間。你也可能會(huì)注意到運(yùn)行時(shí)間加起來不到 3 秒。這是因?yàn)橛锌赡茉?a href='http://www.companysz.com/tag/fuwuqi_6823_1.html' target='_blank'>服務(wù)器上同時(shí)存在其他進(jìn)程,并且腳本沒有 100% 使用 CPU 的整個(gè) 3 秒持續(xù)時(shí)間。5、魔術(shù)常量 PHP 提供了獲取當(dāng)前行號(hào) (__LINE__)、文件路徑 (__FILE__)、目錄路徑 (__DIR__)、函數(shù)名 (__FUNCTION__)、類名 (__CLASS__)、方法名 (__METHOD__) 和命名空間 (__NAMESPACE__) 等有用的魔術(shù)常量。在這篇文章中不作一一介紹,但是我將告訴你一些用例。當(dāng)包含其他腳本文件時(shí),使用 __FILE__ 常量(或者使用 PHP5.3 新具有的 __DIR__ 常量):// this is relative to the loaded script's path// it may cause problems when running scripts from different directoriesrequire_once('config/database.php');// this is always relative to this file's path// no matter where it was included fromrequire_once(dirname(__FILE__) . '/config/database.php');使用 __LINE__ 使得調(diào)試更為輕松。你可以跟蹤到具體行號(hào)。// some code// ...my_debug( some debug message , __LINE__);/* printsLine 4: some debug message// some more code// ...my_debug( another debug message , __LINE__);/* printsLine 11: another debug messagefunction my_debug($msg, $line) { echo Line $line: $msg/n 6、生成唯一標(biāo)識(shí)符某些場景下,可能需要生成一個(gè)唯一的字符串。我看到很多人使用 md5() 函數(shù),即使它并不完全意味著這個(gè)目的:// generate unique stringecho md5(time() . mt_rand(1,1000000));There is actually a PHP function named uniqid() that is meant to be used for this.// generate unique stringecho uniqid();/* prints4bd67c947233e// generate another unique stringecho uniqid();/* prints4bd67c9472340你可能會(huì)注意到,盡管字符串是唯一的,前幾個(gè)字符卻是類似的,這是因?yàn)樯傻淖址c服務(wù)器時(shí)間相關(guān)。但實(shí)際上也存在友好的一方面,由于每個(gè)新生成的 ID 會(huì)按字母順序排列,這樣排序就變得很簡單。為了減少重復(fù)的概率,你可以傳遞一個(gè)前綴,或第二個(gè)參數(shù)來增加熵:// with prefixecho uniqid('foo_');/* printsfoo_4bd67d6cd8b8f// with more entropyecho uniqid('',true);/* prints4bd67d6cd8b926.12135106// bothecho uniqid('bar_',true);/* printsbar_4bd67da367b650.43684647這個(gè)函數(shù)將產(chǎn)生比 md5() 更短的字符串,能節(jié)省一些空間。7、序列化你有沒有遇到過需要在數(shù)據(jù)庫或文本文件存儲(chǔ)一個(gè)復(fù)雜變量的情況?你可能沒能想出一個(gè)格式化字符串并轉(zhuǎn)換成數(shù)組或?qū)ο蟮暮梅椒ǎ琍HP 已經(jīng)為你準(zhǔn)備好此功能。有兩種序列化變量的流行方法。下面是一個(gè)例子,使用 serialize() 和 unserialize() 函數(shù):// a complex array$myvar = array( 'hello', array(1,'two'), 'apple'// convert to a string$string = serialize($myvar);echo $string;/* printsa: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 }// you can reproduce the original variable$newvar = unserialize($string);print_r($newvar);/* printsArray [0] = hello [1] = 42 [2] = Array [0] = 1 [1] = two [3] = apple*/這是原生的 PHP 序列化方法。然而,由于 JSON 近年來大受歡迎,PHP5.2 中已經(jīng)加入了對 JSON 格式的支持?,F(xiàn)在你可以使用 json_encode() 和 json_decode() 函數(shù):// a complex array$myvar = array( 'hello', 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);/* printsArray [0] = hello [1] = 42 [2] = Array [0] = 1 [1] = two [3] = apple這將更為行之有效,尤其與 JavaScript 等許多其他語言兼容。然而對于復(fù)雜的對象,某些信息可能會(huì)丟失。8、壓縮字符串在談到壓縮時(shí),我們通常想到文件壓縮,如 ZIP 壓縮等。在 PHP 中字符串壓縮也是可能的,但不涉及任何壓縮文件。在下面的例子中,我們要利用 gzcompress() 和 gzuncompress() 函數(shù):$string = Lorem ipsum dolor sit amet, consecteturadipiscing elit. Nunc ut elit id mi ultriciesadipiscing. Nulla facilisi. Praesent pulvinar,sapien vel feugiat vestibulum, nulla dui pretium orci,non ultricies elit lacus quis ante. Lorem ipsum dolorsit amet, consectetur adipiscing elit. Aliquampretium ullamcorper urna quis iaculis. Etiam ac massased turpis tempor luctus. Curabitur sed nibh eu elitmollis congue. Praesent ipsum diam, consectetur vitaeornare a, aliquam a nunc. In id magna pellentesquetellus posuere adipiscing. Sed non mi metus, at laciniaaugue. Sed magna nisi, ornare in mollis in, mollissed nunc. Etiam at justo in leo congue mollis.Nullam in neque eget metus hendrerit scelerisqueeu non enim. Ut malesuada lacus eu nulla bibendumid euismod urna sodales. $compressed = gzcompress($string);echo Original size: . strlen($string). /n /* printsOriginal size: 800echo Compressed size: . strlen($compressed). /n /* printsCompressed size: 418// getting it back$original = gzuncompress($compressed);這種操作的壓縮率能達(dá)到 50% 左右。另外的函數(shù) gzencode() 和 gzdecode() 能達(dá)到類似結(jié)果,通過使用不同的壓縮算法。9、注冊停止功能有一個(gè)函數(shù)叫做 register_shutdown_function(),可以讓你在某段腳本完成運(yùn)行之前,執(zhí)行一些指定代碼。假設(shè)你需要在腳本執(zhí)行結(jié)束前捕獲一些基準(zhǔn)統(tǒng)計(jì)信息,例如運(yùn)行的時(shí)間長度:// capture the start time$start_time = microtime(true);// do some stuff// ...// display how long the script tookecho execution took: . (microtime(true) - $start_time). seconds. 這似乎微不足道,你只需要在腳本運(yùn)行的最后添加相關(guān)代碼。但是如果你調(diào)用過 exit() 函數(shù),該代碼將無法運(yùn)行。此外,如果有一個(gè)致命的錯(cuò)誤,或者腳本被用戶意外終止,它可能無法再次運(yùn)行。當(dāng)你使用 register_shutdown_function() 函數(shù),代碼將繼續(xù)執(zhí)行,不論腳本是否停止運(yùn)行:$start_time = microtime(true);register_shutdown_function('my_shutdown');// do some stuff// ...function my_shutdown() { global $start_time; echo execution took: . (microtime(true) - $start_time). seconds. }英文原稿:9 Useful PHP Functions and Features You Need to Know | Nettuts html教程