本文目錄一覽:
10 個經典PHP函數
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」;
php_uname()函數詳解
php_uname — 返回運行 PHP 的系統的有關信息。
語法: php_uname ([$mode])
php_uname() 函數會返回運行 php 的操作系統的相關描述,和 phpinfo() 最頂端上輸出的是同一個字符串。 如果僅僅要獲取操作系統的名稱。可以考慮使用常量 PHP_OS ,不過要注意該常量會包含 PHP 構建 (built) 時的操作系統名。
PHP-bc函數及其應用詳解
bcadd —— 兩個任意精度數字的加法計算 (PHP 4, PHP 5, PHP 7, PHP 8)
bcadd ( string $num1 , string $num2 , ?int $scale = null ): string
註:對 num1 和 num2 求和。
參數:
num1 — 左操作數,字符串類型。
num2 — 右操作數,字符串類型。
scale — 此可選參數用於設置結果中小數點後的小數位數。也可通過使用 bcscale() 來設置全局默認的小數位數,用於所有函數。如果未設置,則默認為 0。 現在 scale 可以為 null。
返回值: 以字符串返回兩個操作數求和之後的結果。
範例:
bcsub —— 兩個任意精度數字的減法 (PHP 4, PHP 5, PHP 7, PHP 8)
bcsub ( string $num1 , string $num2 , ?int $scale = null ): string
註: num1 減去 num2 。
參數:
num1 — 左操作數,字符串類型。
num2 — 右操作數,字符串類型。
scale — 此可選參數用於設置結果中小數點後的小數位數。也可通過使用 bcscale() 來設置全局默認的小數位數,用於所有函數。如果未設置,則默認為 0。 現在 scale 可以為 null。
返回值: 以 string 類型返回減法之後的結果。
範例:
bcmul —— 兩個任意精度數字乘法計算 (PHP 4, PHP 5, PHP 7, PHP 8)
bcmul ( string $num1 , string $num2 , ?int $scale = null ): string
註: num1 乘以 num2 。
參數:
num1 — 左操作數,字符串類型。
num2 — 右操作數,字符串類型。
scale — 此可選參數用於設置結果中小數點後的小數位數。也可通過使用 bcscale() 來設置全局默認的小數位數,用於所有函數。如果未設置,則默認為 0。 現在 scale 可以為 null。
返回值: 以 string 類型返回減法之後的結果。
範例:
bcp —— 兩個任意精度的數字除法計算 (PHP 4, PHP 5, PHP 7, PHP 8)
bcp ( string $num1 , string $num2 , ?int $scale = null ): string
註: num1 除以 num2 。
參數:
num1 — 左操作數,字符串類型。
num2 — 右操作數,字符串類型。
scale — 此可選參數用於設置結果中小數點後的小數位數。也可通過使用 bcscale() 來設置全局默認的小數位數,用於所有函數。如果未設置,則默認為 0。 現在 scale 可以為 null。
返回值: 以 string 類型返回減法之後的結果。
範例:
bccomp —— 比較兩個任意精度的數字 (PHP 4, PHP 5, PHP 7, PHP 8)
bccomp ( string $num1 , string $num2 , ?int $scale = null ): int
註: 比較 num1 和 num2 , 並且返回整型數字的結果。
參數:
num1 — 左邊的運算數,是一個字符串。
num2 — 右邊的運算數,是一個字符串。
scale — 可選的 scale 參數被用作設置指示數字, 在使用來作比較的小數點部分。
返回值: 兩個數相等時返回 0; num1 比 num2 小時返回 -1; 其他則返回 1。現在 scale 可以為 null。
範例:
bcmod —— 任意精度數字取模 (PHP 4, PHP 5, PHP 7, PHP 8)
bcmod ( string $num1 , string $num2 , ?int $scale = null ): string
註: 對 num1 使用 num2 取模。 除非 num2 是零,否則結果必定和 num1 有相同的符號。
參數:
num1 — string 類型的被除數。
num2 — string 類型的除數。
scale — 現在 scale 可以為 null。
返回值: 返回字符串類型取模後的結果,如果 num2 為 0 則返回 null。
範例:
bcpow—— 任意精度數字的乘方 (PHP 4, PHP 5, PHP 7, PHP 8)
bcpow ( string $num , string $exponent , ?int $scale = null ): string
註: num 的 exponent 次方運算。
參數:
num — string 類型的底數。
exponent — string 類型的指數。 如果指數不是整數,將被截斷。 指數的有效範圍取決於平台,但起碼支持 -2147483648 到 2147483647 的範圍。
scale — 此可選參數用於設置結果中小數點後的小數位數。也可通過使用 bcscale() 來設置全局默認的小數位數,用於所有函數。如果未設置,則默認為 0。
返回值: 返回字符串類型的結果。
範例:
bcpowmod —— 先取次方然後 取模 。 (PHP 5, PHP 7, PHP 8)
bcpowmod ( string $num , string $exponent , string $modulus , ?int $scale = null ): string
註: 先取次方然後取模。
參數:
base — 左操作數。它是一個字符串類型的參數。
exponent — string 類型的指數。 指數的正確操作數。
modulus — string 類型的 參 數。 接受表示模數的操作數。
scale — 一個整數類型參數。它說明 ( base exponent %mod ) 結果中小數點後的位數。其默認值為 0。
返回值: 該函數將結果作為字符串返回。或者,如果模數為 0 或指數為負,則返回 False。
範例:
bcscale —— 設置/獲取所有 bc math 函數的默認小數點保留位數 (PHP 4, PHP 5, PHP 7, PHP 8)
bcscale ( int $scale ): int
設置所有 bc math 函數在未設定情況下的小數點保留位數。
bcscale ( null $scale = null ): int
註: 獲取當前的小數點保留位數。
參數:
scale — 小數點保留位數。
返回值: 設置的時候,返回之前的小數點保留位數。否則就是返回當前的位數。
範例:
bcsqrt —— 任意精度數字的二次方根 (PHP 4, PHP 5, PHP 7, PHP 8)
bcsqrt ( string $num , ?int $scale = null ): string
註: 返回 num 的二次方根。
參數:
num — string 類型的操作數 。
scale — 此可選參數用於設置結果中小數點後的小數位數。也可通過使用 bcscale() 來設置全局默認的小數位數,用於所有函數。如果未設置,則默認為 0。
返回值: 以 string 類型返回二次方根的結果,如果 num 是負數則返回 null。
範例:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/239615.html