先容10个很是有效的PHP函数

时间:2014-02-15所属栏目: PHP/PHP框架 来源: 未知 热度:

函数是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()
这是一个调试用的函数,能辅佐你发明代码中的逻辑错误。要理 解这个函数,照旧直接看个例子吧:

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

7、metaphone()
这个函数返回单词的metaphone值,沟通读音的单词具有沟通的metaphone值,也就是嗣魅这个函数可以帮你判定两个单词的读音是否 沟通。

8、natsort()
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   
# ) 
?>

9、levenshtein()
Levenshtein()汇报你两个单词之间的”间隔”。它汇报你假如想把一个单词酿成另一个单词,必要插入、替代和删除几多字母。

标签:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)