《PHP学习:10个php函数实用却不常见》要点:
本文介绍了PHP学习:10个php函数实用却不常见,希望对您有用。如果有疑问,可以联系我们。
PHP实战1. sys_getloadavg()
PHP实战sys_getloadavt()可以获得系 统负载情况.该函数返回一个包括三个元素的数组,每个元素分别代表系统再过去的1、5和15分钟内的平均负载.与其让服务器因负 载过高而宕掉,不如在系统负载很高时主动die掉一个脚本,sys_getloadavg()就是用来帮你实现这个功能的. 不过很遗憾,该函数在windows下无效.
PHP实战2. pack()
PHP实战Pack() 能将md5()返回的32位16进制字符串转换为16位的二进制字符串,可以节俭存储空间.
PHP实战3. cal_days_in_month()
PHP实战cal_days_in_month()能够返回指定月份共有若干天.
PHP实战4. _()
PHP实战WordPress开发者经常能见到这个函数,还有 _e().这两个函数功能相同,与gettext()函数结合使用,能实现网站的多语言化.具体可参见PHP手册的相关部门介绍.
PHP实战5. get_browser()
PHP实战在发送页面前先看看用户的浏览器都能做些什么是 不是挺好?get_browser()能获得用户的浏览器类型,以及浏览器支持的功能,不过首先你必要一个php_browscap.ini文件,用来给 函数做参考文件.
PHP实战要注意,该函数对浏览器功能的判断是基于该类浏览器的一般特性的.例如,如果用户关闭了浏览器对 JavaScript的支持,函数无法得知这一点.然则在判断浏览器类型和OS平台方面,该函数还是很准确的.
PHP实战6. debug_print_backtrace()
PHP实战这是一个调试用的函数,能赞助你发现代码中的逻辑错误.要理 解这个函数,还是直接看个例子吧:
- PHP实战
- $a = 0;
- function iterate() {
- global $a;
- if( $a < 10 )
- recur();
- echo $a . “, “;
- }
- function recur() {
- global $a;
- $a++;
- // how did I get here?
- echo “\n\n\n”;
- debug_print_backtrace();
- if( $a < 10 )
- iterate();
- }
- iterate();
- # OUTPUT:
- #0 recur() called at [C:\htdocs\php_stuff\index.php:8]
- #1 iterate() called at [C:\htdocs\php_stuff\index.php:25]
- #0 recur() called at [C:\htdocs\php_stuff\index.php:8]
- #1 iterate() called at [C:\htdocs\php_stuff\index.php:21]
- #2 recur() called at [C:\htdocs\php_stuff\index.php:8]
- #3 iterate() called at [C:\htdocs\php_stuff\index.php:25]
- #0 recur() called at [C:\htdocs\php_stuff\index.php:8]
- #1 iterate() called at [C:\htdocs\php_stuff\index.php:21]
- #2 recur() called at [C:\htdocs\php_stuff\index.php:8]
- #3 iterate() called at [C:\htdocs\php_stuff\index.php:21]
- #4 recur() called at [C:\htdocs\php_stuff\index.php:8]
- #5 iterate() called at [C:\htdocs\php_stuff\index.php:25]
PHP实战7. metaphone()
PHP实战这个函数返回单词的metaphone值,相同读音的单词具有相同的metaphone值,也便是说这个函数可以帮你判断两个单词的读音是否 相同.
PHP实战8. natsort()
PHP实战natsort()能将一个数组以自然排序法 进行分列,直接看个例子吧:
- PHP实战
- $items = array(
- “100 apples”, “5 apples”, “110 apples”, “55 apples”
- );
- // normal sorting:
- sort($items);
- print_r($items);
- # Outputs:
- # Array
- # (
- # [0] => 100 apples
- # [1] => 110 apples
- # [2] => 5 apples
- # [3] => 55 apples
- # )
- natsort($items);
- print_r($items);
- # Outputs:
- # Array
- # (
- # [2] => 5 apples
- # [3] => 55 apples
- # [0] => 100 apples
- # [1] => 110 apples
- # )
PHP实战9. levenshtein()
PHP实战Levenshtein()告诉你两个单词之间的“距离”.它告诉你如果想把一个单词变成另一个单词,必要插入、替换和删除多少字母.
PHP实战看个例子吧:
- PHP实战
- $dictionary = array(
- “php”, “javascript”, “css”
- );
- $word = “japhp”;
- $best_match = $dictionary[0];
- $match_value = levenshtein($dictionary[0], $word);
- foreach($dictionary as $w) {
- $value = levenshtein($word, $w);
- if( $value < $match_value ) {
- $best_match = $w;
- $match_value = $value;
- }
- }
- echo “Did you mean the ‘$best_match' category?”;
PHP实战10. glob()
PHP实战glob()会让你觉得用 opendir(), readdir()和closedir()来寻找文件异常蠢.
- PHP实战
- foreach (glob(“*.php”) as $file)
- echo “$file\n”;
PHP实战10个php函数适用却不常见,快点收藏起来吧
欢迎参与《PHP学习:10个php函数实用却不常见》讨论,分享您的想法,维易PHP学院为您提供专业教程。
转载请注明本页网址:
http://www.vephp.com/jiaocheng/8589.html