PHP中10個(gè)不常見卻非常有用的函數(shù)
更新時(shí)間:2010年03月21日 15:55:16 作者:
PHP中10個(gè)雖然不常見卻非常有用的函數(shù),對(duì)于有特殊需求的朋友可以參考下,充分發(fā)揮php的優(yōu)勢(shì)。
1. sys_getloadavg()
sys_getloadavt()可以獲得系統(tǒng)負(fù)載情況。該函數(shù)返回一個(gè)包含三個(gè)元素的數(shù)組,每個(gè)元素分別代表系統(tǒng)再過去的1、5和15分鐘內(nèi)的平均負(fù)載。
與其讓服務(wù)器因負(fù)載過高而宕掉,不如在系統(tǒng)負(fù)載很高時(shí)主動(dòng)die掉一個(gè)腳本,sys_getloadavg()就是用來幫你實(shí)現(xiàn)這個(gè)功能的。 不過很遺憾,該函數(shù)在windows下無效。
2. pack()
Pack()能將md5()返回的32位16進(jìn)制字符串轉(zhuǎn)換為16位的二進(jìn)制字符串,可以節(jié)省存儲(chǔ)空間。
3. cal_days_in_month()
cal_days_in_month()能夠返回指定月份共有多少天。
4. _()
WordPress開發(fā)者經(jīng)常能見到這個(gè)函數(shù),還有_e()。這兩個(gè)函數(shù)功能相同,與gettext()函數(shù)結(jié)合使用,能實(shí)現(xiàn)網(wǎng)站的多語(yǔ)言化。具體可參見PHP手冊(cè)的相關(guān)部分介紹。
5. get_browser()
在發(fā)送頁(yè)面前先看看用戶的瀏覽器都能做些什么是不是挺好?get_browser()能獲得用戶的瀏覽器類型,以及瀏覽器支持的功能,不過首先你需要一個(gè)php_browscap.ini文件,用來給函數(shù)做參考文件。
要注意,該函數(shù)對(duì)瀏覽器功能的判斷是基于該類瀏覽器的一般特性的。例如,如果用戶關(guān)閉了瀏覽器對(duì)JavaScript的支持,函數(shù)無法得知這一點(diǎn)。但是在判斷瀏覽器類型和OS平臺(tái)方面,該函數(shù)還是很準(zhǔn)確的。
6. debug_print_backtrace()
這是一個(gè)調(diào)試用的函數(shù),能幫助你發(fā)現(xiàn)代碼中的邏輯錯(cuò)誤。要理解這個(gè)函數(shù),還是直接看個(gè)例子吧:
$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()
這個(gè)函數(shù)返回單詞的metaphone值,相同讀音的單詞具有相同的metaphone值,也就是說這個(gè)函數(shù)可以幫你判斷兩個(gè)單詞的讀音是否相同。不過對(duì)中文就無效了。。。
8. natsort()
natsort()能將一個(gè)數(shù)組以自然排序法進(jìn)行排列,直接看個(gè)例子吧:
$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()告訴你兩個(gè)單詞之間的“距離”。它告訴你如果想把一個(gè)單詞變成另一個(gè)單詞,需要插入、替換和刪除多少字母。
看個(gè)例子吧:
$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()會(huì)讓你覺得用opendir(), readdir()和closedir()來尋找文件非常蠢。
foreach (glob(“*.php”) as $file)
echo “$file\n”;
sys_getloadavt()可以獲得系統(tǒng)負(fù)載情況。該函數(shù)返回一個(gè)包含三個(gè)元素的數(shù)組,每個(gè)元素分別代表系統(tǒng)再過去的1、5和15分鐘內(nèi)的平均負(fù)載。
與其讓服務(wù)器因負(fù)載過高而宕掉,不如在系統(tǒng)負(fù)載很高時(shí)主動(dòng)die掉一個(gè)腳本,sys_getloadavg()就是用來幫你實(shí)現(xiàn)這個(gè)功能的。 不過很遺憾,該函數(shù)在windows下無效。
2. pack()
Pack()能將md5()返回的32位16進(jìn)制字符串轉(zhuǎn)換為16位的二進(jìn)制字符串,可以節(jié)省存儲(chǔ)空間。
3. cal_days_in_month()
cal_days_in_month()能夠返回指定月份共有多少天。
4. _()
WordPress開發(fā)者經(jīng)常能見到這個(gè)函數(shù),還有_e()。這兩個(gè)函數(shù)功能相同,與gettext()函數(shù)結(jié)合使用,能實(shí)現(xiàn)網(wǎng)站的多語(yǔ)言化。具體可參見PHP手冊(cè)的相關(guān)部分介紹。
5. get_browser()
在發(fā)送頁(yè)面前先看看用戶的瀏覽器都能做些什么是不是挺好?get_browser()能獲得用戶的瀏覽器類型,以及瀏覽器支持的功能,不過首先你需要一個(gè)php_browscap.ini文件,用來給函數(shù)做參考文件。
要注意,該函數(shù)對(duì)瀏覽器功能的判斷是基于該類瀏覽器的一般特性的。例如,如果用戶關(guān)閉了瀏覽器對(duì)JavaScript的支持,函數(shù)無法得知這一點(diǎn)。但是在判斷瀏覽器類型和OS平臺(tái)方面,該函數(shù)還是很準(zhǔn)確的。
6. debug_print_backtrace()
這是一個(gè)調(diào)試用的函數(shù),能幫助你發(fā)現(xiàn)代碼中的邏輯錯(cuò)誤。要理解這個(gè)函數(shù),還是直接看個(gè)例子吧:
復(fù)制代碼 代碼如下:
$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()
這個(gè)函數(shù)返回單詞的metaphone值,相同讀音的單詞具有相同的metaphone值,也就是說這個(gè)函數(shù)可以幫你判斷兩個(gè)單詞的讀音是否相同。不過對(duì)中文就無效了。。。
8. natsort()
natsort()能將一個(gè)數(shù)組以自然排序法進(jìn)行排列,直接看個(gè)例子吧:
復(fù)制代碼 代碼如下:
$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()告訴你兩個(gè)單詞之間的“距離”。它告訴你如果想把一個(gè)單詞變成另一個(gè)單詞,需要插入、替換和刪除多少字母。
看個(gè)例子吧:
復(fù)制代碼 代碼如下:
$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()會(huì)讓你覺得用opendir(), readdir()和closedir()來尋找文件非常蠢。
復(fù)制代碼 代碼如下:
foreach (glob(“*.php”) as $file)
echo “$file\n”;
相關(guān)文章
PHP開發(fā)不能違背的安全規(guī)則 過濾用戶輸入
作為PHP程序員,特別是新手,對(duì)于互聯(lián)網(wǎng)的險(xiǎn)惡總是知道的太少,對(duì)于外部的入侵有很多時(shí)候是素手無策的,他們根本不知道黑客是如何入侵的、提交入侵、上傳漏洞、sql 注入、跨腳本攻擊等等。2011-05-05
php 隨機(jī)記錄mysql rand()造成CPU 100%的解決辦法
mysql數(shù)據(jù)庫(kù)有10幾萬條數(shù)據(jù),使用rand()提取隨機(jī)10條記錄,導(dǎo)致服務(wù)器cpu占用居高不下直至死機(jī)~2010-05-05
解析web文件操作常見安全漏洞(目錄、文件名檢測(cè)漏洞)
本篇文章是對(duì)web文件操作常見安全漏洞(目錄、文件名檢測(cè)漏洞)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP使用http_build_query()構(gòu)造URL字符串的方法
這篇文章主要介紹了PHP使用http_build_query()構(gòu)造URL字符串的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了http_build_query函數(shù)的功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-04-04
php中如何使對(duì)象可以像數(shù)組一樣進(jìn)行foreach循環(huán)
php中如何使對(duì)象可以像數(shù)組一樣進(jìn)行foreach循環(huán)呢?下面小編就詳細(xì)的為大家介紹一下吧!需要的朋友可以 過來參考下2013-08-08

