教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

PHP培訓實戰(zhàn)教程之PHP函數(shù)

更新時間:2016年01月27日10時28分 來源:傳智播客php培訓學院 瀏覽次數(shù):

 

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

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

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

4. _()
WordPress開發(fā)者經(jīng)常能見到這個函數(shù),還有_e()。這兩個函數(shù)功能相同,與gettext()函數(shù)結合使用,能實現(xiàn)網(wǎng)站的多語言化。

5. get_browser()
在發(fā)送頁面前先看看用戶的瀏覽器都能做些什么是不是挺好?get_browser()能獲得用戶的瀏覽器類型,以及瀏覽器支持的功能,不過首先你需要一個php_browscap.ini文件,用來給函數(shù)做參考文件。
要注意,該函數(shù)對瀏覽器功能的判斷是基于該類瀏覽器的一般特性的。例如,如果用戶關閉了瀏覽器對JavaScript的支持,函數(shù)無法得知這一點。但是在判斷瀏覽器類型和OS平臺方面,該函數(shù)還是很準確的。

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

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

7. metaphone()
這個函數(shù)返回單詞的metaphone值,相同讀音的單詞具有相同的metaphone值,也就是說這個函數(shù)可以幫你判斷兩個單詞的讀音是否相同。不過對中文就無效了。。。

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

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

9. levenshtein()
Levenshtein()告訴你兩個單詞之間的“距離”。它告訴你如果想把一個單詞變成另一個單詞,需要插入、替換和刪除多少字母。
看個我們在PHP培訓實踐中的例子吧:

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

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

看了這些函數(shù),你是不是感覺輕松多了?php培訓學院始終認為你將會是最棒的,加油!


本文版權歸傳智播客php培訓學院所有,歡迎轉載,轉載請注明作者出處。謝謝!
作者:傳智播客php培訓學院

0 分享到:
和我們在線交談!