函数是PHP如此强大的源泉,但是很多PHP函数并没有得到充分的利用。这里,我们给大家简单介绍10个不常见,但非常有用的函数。
1、sys_getloadavg()
sys_getloadavt()可以获得系统负载情况。该函数返回一个包含三个元素的数组,每个元素分别代表系统再过去的1、5和15分钟内的平均负载。与其让服务器因负 载过高而宕掉,不如在系统负载很高时主动die掉一个脚本,sys_getloadavg()就是用来帮你实现这个功能的。 不过很遗憾,该函数在windows下无效。
2、pack()
Pack() 能将md5()返回的32位16进制字符串转换为16位的二进制字符串,可以节省存储空间。
3、cal_days_in_month()
cal_days_in_month()能够返回指定月份共有多少天。
4、_()
WordPress开发者经常能见到这个函数,还有 _e()。这两个函数功能相同,与gettext()函数结合使用,能实现网站的多语言化。具体可参见PHP手册的相关部分介绍。
5、get_browser()
在发送页面前先看看用户的浏览器都能做些什么是 不是挺好?get_browser()能获得用户的浏览器类型,以及浏览器支持的功能,不过首先你需要一个php_browscap.ini文件,用来给 函数做参考文件。
要注意,该函数对浏览器功能的判断是基于该类浏览器的一般特性的。例如,如果用户关闭了浏览器对 JavaScript的支持,函数无法得知这一点。但是在判断浏览器类型和OS平台方面,该函数还是很准确的。
6、debug_print_backtrace()
这是一个调试用的函数,能帮助你发现代码中的逻辑错误。要理 解这个函数,还是直接看个例子吧:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$a</span><span> = 0; </span></span></li> <li> <span class="keyword">function</span><span> iterate() { </span> </li> <li class="alt"> <span class="keyword">global</span><span> </span><span class="vars">$a</span><span>; </span> </li> <li> <span class="keyword">if</span><span>( </span><span class="vars">$a</span><span> </span> </li> <li class="alt"><span>recur(); </span></li> <li> <span class="func">echo</span><span> </span><span class="vars">$a</span><span> . “, “; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="keyword">function</span><span> recur() { </span> </li> <li class="alt"> <span class="keyword">global</span><span> </span><span class="vars">$a</span><span>; </span> </li> <li> <span class="vars">$a</span><span>++; </span> </li> <li class="alt"> <span class="comment">// how did I get here? </span><span> </span> </li> <li> <span class="func">echo</span><span> “nnn”; </span> </li> <li class="alt"><span>debug_print_backtrace(); </span></li> <li> <span class="keyword">if</span><span>( </span><span class="vars">$a</span><span> </span> </li> <li class="alt"><span>iterate(); </span></li> <li><span>} </span></li> <li class="alt"><span>iterate(); </span></li> <li><span># OUTPUT: </span></li> <li class="alt"><span>#0 recur() called at [C:htdocsphp_stuffindex.php:8] </span></li> <li><span>#1 iterate() called at [C:htdocsphp_stuffindex.php:25] </span></li> <li class="alt"><span>#0 recur() called at [C:htdocsphp_stuffindex.php:8] </span></li> <li><span>#1 iterate() called at [C:htdocsphp_stuffindex.php:21] </span></li> <li class="alt"><span>#2 recur() called at [C:htdocsphp_stuffindex.php:8] </span></li> <li><span>#3 iterate() called at [C:htdocsphp_stuffindex.php:25] </span></li> <li class="alt"><span>#0 recur() called at [C:htdocsphp_stuffindex.php:8] </span></li> <li><span>#1 iterate() called at [C:htdocsphp_stuffindex.php:21] </span></li> <li class="alt"><span>#2 recur() called at [C:htdocsphp_stuffindex.php:8] </span></li> <li><span>#3 iterate() called at [C:htdocsphp_stuffindex.php:21] </span></li> <li class="alt"><span>#4 recur() called at [C:htdocsphp_stuffindex.php:8] </span></li> <li><span>#5 iterate() called at [C:htdocsphp_stuffindex.php:25] </span></li> </ol>
7、metaphone()
这个函数返回单词的metaphone值,相同读音的单词具有相同的metaphone值,也就是说这个函数可以帮你判断两个单词的读音是否 相同。
8、natsort()
natsort()能将一个数组以自然排序法 进行排列,直接看个例子吧:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$items</span><span> = </span><span class="keyword">array</span><span>( </span></span></li> <li><span>“100 apples”, “5 apples”, “110 apples”, “55 apples” </span></li> <li class="alt"><span>); </span></li> <li> <span class="comment">// normal sorting: </span><span> </span> </li> <li class="alt"> <span>sort(</span><span class="vars">$items</span><span>); </span> </li> <li> <span>print_r(</span><span class="vars">$items</span><span>); </span> </li> <li class="alt"><span># Outputs: </span></li> <li><span># Array </span></li> <li class="alt"><span># ( </span></li> <li><span># [0] => 100 apples </span></li> <li class="alt"><span># [1] => 110 apples </span></li> <li><span># [2] => 5 apples </span></li> <li class="alt"><span># [3] => 55 apples </span></li> <li><span># ) </span></li> <li class="alt"> <span>natsort(</span><span class="vars">$items</span><span>); </span> </li> <li> <span>print_r(</span><span class="vars">$items</span><span>); </span> </li> <li class="alt"><span># Outputs: </span></li> <li><span># Array </span></li> <li class="alt"><span># ( </span></li> <li><span># [2] => 5 apples </span></li> <li class="alt"><span># [3] => 55 apples </span></li> <li><span># [0] => 100 apples </span></li> <li class="alt"><span># [1] => 110 apples </span></li> <li><span># ) </span></li> </ol>
9、levenshtein()
Levenshtein()告诉你两个单词之间的“距离”。它告诉你如果想把一个单词变成另一个单词,需要插入、替换和删除多少字母。
看个例子吧:
<ol class="dp-c"> <li class="alt"><span><span class="vars">$dictionary</span><span> = </span><span class="keyword">array</span><span>( </span></span></li> <li><span>“php”, “javascript”, “css” </span></li> <li class="alt"><span>); </span></li> <li> <span class="vars">$word</span><span> = “japhp”; </span> </li> <li class="alt"> <span class="vars">$best_match</span><span> = </span><span class="vars">$dictionary</span><span>[0]; </span> </li> <li> <span class="vars">$match_value</span><span> = levenshtein(</span><span class="vars">$dictionary</span><span>[0], </span><span class="vars">$word</span><span>); </span> </li> <li class="alt"> <span class="keyword">foreach</span><span>(</span><span class="vars">$dictionary</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$w</span><span>) { </span> </li> <li> <span class="vars">$value</span><span> = levenshtein(</span><span class="vars">$word</span><span>, </span><span class="vars">$w</span><span>); </span> </li> <li class="alt"> <span class="keyword">if</span><span>( </span><span class="vars">$value</span><span> <span class="vars">$match_value</span><span> ) { </span></span> </li> <li> <span class="vars">$best_match</span><span> = </span><span class="vars">$w</span><span>; </span> </li> <li class="alt"> <span class="vars">$match_value</span><span> = </span><span class="vars">$value</span><span>; </span> </li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li> <span class="func">echo</span><span> “Did you mean the ‘</span><span class="vars">$best_match</span><span>’ category?”; </span> </li> </ol>
10、glob()
glob()会让你觉得用 opendir(), readdir()和closedir()来寻找文件非常蠢。
<ol class="dp-c"> <li class="alt"><span><span class="keyword">foreach</span><span> (</span><span class="func">glob</span><span>(“*.php”) </span><span class="keyword">as</span><span> </span><span class="vars">$file</span><span>) </span></span></li> <li> <span class="func">echo</span><span> “</span><span class="vars">$file</span><span>n”; </span> </li> </ol>
希望本文介绍的这10个PHP函数,能够帮助到你,更好的使用PHP。