自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

不常見卻非常有用的PHP函數(shù)

開發(fā) 后端
函數(shù)是PHP如此強(qiáng)大的源泉,但是很多PHP函數(shù)并沒有得到充分的利用。這里,我們給大家簡單介紹10個不常見,但非常有用的函數(shù)。

函數(shù)是PHP如此強(qiáng)大的源泉,但是很多PHP函數(shù)并沒有得到充分的利用。這里,我們給大家簡單介紹10個不常見,但非常有用的函數(shù)。

51CTO推薦閱讀:那些未曾了解的PHP函數(shù)和功能

1. sys_getloadavg()

sys_getloadavt()可以獲得系 統(tǒng)負(fù)載情況。該函數(shù)返回一個包含三個元素的數(shù)組,每個元素分別代表系統(tǒng)再過去的1、5和15分鐘內(nèi)的平均負(fù)載。與其讓服務(wù)器因負(fù) 載過高而宕掉,不如在系統(tǒng)負(fù)載很高時主動die掉一個腳本,sys_getloadavg()就是用來幫你實現(xiàn)這個功能的。 不過很遺憾,該函數(shù)在windows下無效。

2. pack()

Pack() 能將md5()返回的32位16進(jìn)制字符串轉(zhuǎn)換為16位的二進(jìn)制字符串,可以節(jié)省存儲空間。

3. cal_days_in_month()

cal_days_in_month()能夠返回指定月份共有多少天。

4. _()

WordPress開發(fā)者經(jīng)常能見到這個函數(shù),還有 _e()。這兩個函數(shù)功能相同,與gettext()函數(shù)結(jié)合使用,能實現(xiàn)網(wǎng)站的多語言化。具體可參見PHP手冊的相關(guān)部分介紹。

5. get_browser()

在發(fā)送頁面前先看看用戶的瀏覽器都能做些什么是 不是挺好?get_browser()能獲得用戶的瀏覽器類型,以及瀏覽器支持的功能,不過首先你需要一個php_browscap.ini文件,用來給 函數(shù)做參考文件。

要注意,該函數(shù)對瀏覽器功能的判斷是基于該類瀏覽器的一般特性的。例如,如果用戶關(guān)閉了瀏覽器對 JavaScript的支持,函數(shù)無法得知這一點。但是在判斷瀏覽器類型和OS平臺方面,該函數(shù)還是很準(zhǔn)確的。

6. debug_print_backtrace()

這是一個調(diào)試用的函數(shù),能幫助你發(fā)現(xiàn)代碼中的邏輯錯誤。要理 解這個函數(shù),還是直接看個例子吧:

  1. $a = 0;   
  2. function iterate() {   
  3. global $a;   
  4. if( $a < 10 )   
  5. recur();   
  6. echo $a . “, “;   
  7. }   
  8. function recur() {   
  9. global $a;   
  10. $a++;   
  11. // how did I get here?   
  12. echo “\n\n\n”;   
  13. debug_print_backtrace();   
  14. if( $a < 10 )   
  15. iterate();   
  16. }   
  17. iterate();   
  18. # OUTPUT:   
  19. #0 recur() called at [C:\htdocs\php_stuff\index.php:8]   
  20. #1 iterate() called at [C:\htdocs\php_stuff\index.php:25]   
  21. #0 recur() called at [C:\htdocs\php_stuff\index.php:8]   
  22. #1 iterate() called at [C:\htdocs\php_stuff\index.php:21]   
  23. #2 recur() called at [C:\htdocs\php_stuff\index.php:8]   
  24. #3 iterate() called at [C:\htdocs\php_stuff\index.php:25]   
  25. #0 recur() called at [C:\htdocs\php_stuff\index.php:8]   
  26. #1 iterate() called at [C:\htdocs\php_stuff\index.php:21]   
  27. #2 recur() called at [C:\htdocs\php_stuff\index.php:8]   
  28. #3 iterate() called at [C:\htdocs\php_stuff\index.php:21]   
  29. #4 recur() called at [C:\htdocs\php_stuff\index.php:8]   
  30. #5 iterate() called at [C:\htdocs\php_stuff\index.php:25]  

7. metaphone()

這個函數(shù)返回單詞的metaphone值,相同讀音的單詞具有相同的metaphone值,也就是說這個函數(shù)可以幫你判斷兩個單詞的讀音是否 相同。

8. natsort()

natsort()能將一個數(shù)組以自然排序法 進(jìn)行排列,直接看個例子吧:

  1. $items = array(   
  2. “100 apples”, “5 apples”, “110 apples”, “55 apples”   
  3. );   
  4. // normal sorting:   
  5. sort($items);   
  6. print_r($items);   
  7. # Outputs:   
  8. # Array   
  9. # (   
  10. # [0] => 100 apples   
  11. # [1] => 110 apples   
  12. # [2] => 5 apples   
  13. # [3] => 55 apples   
  14. # )   
  15. natsort($items);   
  16. print_r($items);   
  17. # Outputs:   
  18. # Array   
  19. # (   
  20. # [2] => 5 apples   
  21. # [3] => 55 apples   
  22. # [0] => 100 apples   
  23. # [1] => 110 apples   
  24. # )  

9. levenshtein()

Levenshtein()告訴你兩個單詞之間的“距離”。它告訴你如果想把一個單詞變成另一個單詞,需要插入、替換和刪除多少字母。

看個例子吧:

  1. $dictionary = array(   
  2. “php”, “javascript”, “css”   
  3. );   
  4. $word = “japhp”;   
  5. $best_match = $dictionary[0];   
  6. $match_value = levenshtein($dictionary[0], $word);   
  7. foreach($dictionary as $w) {   
  8. $value = levenshtein($word, $w);   
  9. if( $value < $match_value ) {   
  10. $best_match = $w;   
  11. $match_value = $value;   
  12. }   
  13. }   
  14. echo “Did you mean the ‘$best_match’ category?”;  

10. glob()

glob()會讓你覺得用 opendir(), readdir()和closedir()來尋找文件非常蠢。

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

 

【編輯推薦】

  1. 深入理解PHP中的匿名函數(shù)
  2. 那些未曾了解的PHP函數(shù)和功能
  3. 為你總結(jié)一些PHP信息函數(shù) 
責(zé)任編輯:王曉東 來源: 百度空間
相關(guān)推薦

2021-10-21 22:03:00

PythonNumpy函數(shù)

2011-07-05 11:24:52

SQL語句索引

2011-07-07 17:16:43

PHP

2021-08-17 10:34:19

Python數(shù)據(jù)科學(xué)機(jī)器學(xué)習(xí)

2009-03-24 14:23:59

PHP類庫PHP開發(fā)PHP

2014-02-09 09:50:49

PHP函數(shù)

2020-10-29 10:00:55

Python函數(shù)文件

2023-06-13 15:15:02

JavaScript前端編程語言

2013-06-14 14:57:09

Java基礎(chǔ)代碼

2017-08-02 13:32:18

編程Java程序片段

2011-04-06 14:08:14

jQuery

2023-02-19 15:22:22

React技巧

2012-05-25 14:20:08

JavaScript

2022-09-02 23:08:04

JavaScript技巧開發(fā)

2021-10-30 18:59:15

Python

2018-08-03 10:02:05

Linux命令

2021-07-06 11:25:20

Chrome前端代碼

2013-11-05 10:03:22

Eclipse功能

2013-08-21 10:31:22

HTML5工具

2013-08-12 15:00:24

LinuxLinux命令
點贊
收藏

51CTO技術(shù)棧公眾號