歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 10個非常有用的PHP函數

10個非常有用的PHP函數

日期:2017/3/1 10:13:31   编辑:Linux編程

函數是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] ?>
Copyright © Linux教程網 All Rights Reserved