PHP:10個不常見卻非常有用的PHP函數

來源:互聯網
上載者:User
  • 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”;

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.