10 个经典PHP函数


Posted in PHP onOctober 17, 2013

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()

这是一个调试用的函数,能帮助你发现代码中的逻辑错误。要理 解这个函数,还是直接看个例子吧:

$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]

7. metaphone()

这个函数返回单词的metaphone值,相同读音的单词具有相同的metaphone值,也就是说这个函数可以帮你判断两个单词的读音是否 相同。不过对中文就无效了。。。

8. natsort()

natsort()能将一个数组以自然排序法 进行排列,直接看个例子吧:

$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
# )

9. levenshtein()

Levenshtein() 告诉你两个单词之间的“距离”。它告诉你如果想把一个单词变成另一个单词,需要插入、替换和删除多少字母。

看个例子吧:

$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?”;

10. glob()

glob()会让你觉得用 opendir(), readdir()和closedir()来寻找文件非常蠢。

foreach (glob(“*.php”) as $file)
echo “$file\n”;

PHP 相关文章推荐
收集的php编写大型网站问题集
Mar 06 PHP
ThinkPHP CURD方法之order方法详解
Jun 18 PHP
Parse正式发布开源PHP SDK
Aug 11 PHP
php获取根域名方法汇总
Oct 28 PHP
php检查字符串中是否包含7位GSM字符的方法
Mar 17 PHP
在WordPress中获取数据库字段内容和添加主题设置菜单
Jan 11 PHP
PHP+Mysql+Ajax实现淘宝客服或阿里旺旺聊天功能(前台页面)
Jun 16 PHP
PHP7如何开启Opcode打造强悍性能详解
May 11 PHP
使用PHP访问RabbitMQ消息队列的方法示例
Jun 06 PHP
php面向对象重点知识分享
Sep 27 PHP
如何在Laravel5.8中正确地应用Repository设计模式
Nov 26 PHP
PHP数据源架构模式之表入口模式实例分析
Jan 23 PHP
php中怎么搜索相关联数组键值及获取之
Oct 17 #PHP
php class类的用法详细总结
Oct 17 #PHP
div li的多行多列 无刷新分页示例代码
Oct 16 #PHP
无刷新动态加载数据 滚动条加载适合评论等页面
Oct 16 #PHP
PHP字符串长度计算 - strlen()函数使用介绍
Oct 15 #PHP
php后台如何避免用户直接进入方法实例
Oct 15 #PHP
php下拉选项的批量操作的实现代码
Oct 14 #PHP
You might like
国王的咖啡这么大来头,名字的由来是什么
2021/03/03 咖啡文化
php注入实例
2006/10/09 PHP
php使用fsockopen函数发送post,get请求获取网页内容的方法
2014/11/15 PHP
PHP5.2下preg_replace函数的问题
2015/05/08 PHP
PHP读取大文件的多种方法介绍
2016/04/04 PHP
PHP+Ajax实现的检测用户名功能简单示例
2019/02/12 PHP
PHP实现笛卡尔积算法的实例讲解
2019/12/22 PHP
通过实例解析PHP数据类型转换方法
2020/07/11 PHP
js对象转json数组的简单实现案例
2014/02/28 Javascript
js创建对象的方法汇总
2016/01/07 Javascript
jQuery给div,Span, a ,button, radio 赋值与取值
2016/06/24 Javascript
Vue.js计算属性computed与watch(5)
2016/12/09 Javascript
Javascript中的 “&amp;” 和 “|” 详解
2017/02/02 Javascript
VsCode插件整理(小结)
2017/09/14 Javascript
解析Vue.js中的组件
2018/02/02 Javascript
微信小程序中换行空格(多个空格)写法详解
2018/07/10 Javascript
详解NodeJS Https HSM双向认证实现
2019/03/12 NodeJs
Element Collapse 折叠面板的使用方法
2020/07/26 Javascript
Linux下使用python自动修改本机网关代码分享
2015/05/21 Python
Python开发如何在ubuntu 15.10 上配置vim
2016/01/25 Python
python判断字符串编码的简单实现方法(使用chardet)
2016/07/01 Python
Python编程实现二叉树及七种遍历方法详解
2017/06/02 Python
Python利用matplotlib做图中图及次坐标轴的实例
2019/07/08 Python
Python二次规划和线性规划使用实例
2019/12/09 Python
html5的pushstate以及监听浏览器返回事件的实现
2020/08/11 HTML / CSS
美国围栏公司:Walpole Outdoors
2019/11/19 全球购物
ECHT官方网站:男女健身服
2020/02/14 全球购物
电工技术比武方案
2014/05/11 职场文书
优秀毕业生自荐信
2014/06/10 职场文书
中学生教师节演讲稿
2014/09/03 职场文书
涉外离婚协议书怎么写
2014/11/20 职场文书
爱牙日宣传活动总结
2015/02/05 职场文书
迎新生晚会主持词
2015/06/30 职场文书
男方家长婚礼答谢词
2015/09/29 职场文书
详解CSS伪元素的妙用单标签之美
2021/05/25 HTML / CSS
Ubuntu Server 安装Tomcat并配置systemctl
2022/04/28 Servers