9個必須掌握的實用PHP函數和功能

來源:互聯網
上載者:User
本文出拓勝科技,請關注我的toceansoft
即使使用 PHP 多年,也會偶然發現一些未曾瞭解的函數和功能。其中有些是非常有用的,但沒有得到充分利用。並不是所有人都會從頭到尾一頁一頁地閱讀手冊和函數參考!1、任意參數數目的函數你可能已經知道,PHP 允許定義選擇性參數的函數。但也有完全允許任意數目的函數參數的方法。以下是選擇性參數的例子:// function with 2 optional arguments
function foo($arg1 = ”, $arg2 = ”) {echo “arg1: $arg1n”;
echo “arg2: $arg2n”;}foo(‘hello’,'world’);
/* prints:
arg1: hello
arg2: world
*/foo();
/* prints:
arg1:
arg2:
*/現在讓我們看看如何建立能夠接受任何參數數目的函數。這一次需要使用 func_get_args() 函數:// yes, the argument list can be empty
function foo() {// returns an array of all passed arguments
$args = func_get_args();foreach ($args as $k => $v) {
echo “arg”.($k+1).”: $vn”;
}}foo();
/* prints nothing */foo(‘hello’);
/* prints
arg1: hello
*/foo(‘hello’, ‘world’, ‘again’);
/* prints
arg1: hello
arg2: world
arg3: again
*/2、使用 Glob() 尋找檔案許多 PHP 函數具有長描述性的名稱。然而可能會很難說出 glob() 函數能做的事情,除非你已經通過多次使用並熟悉了它。可以把它看作是比 scandir() 函數更強大的版本,可以按照某種模式搜尋檔案。// get all php files
$files = glob(‘*.php’);print_r($files);
/* output looks like:
Array
(
[0] => phptest.php
[1] => pi.php
[2] => post_output.php
[3] => test.php
)
*/你可以像這樣獲得多個檔案:// get all php files AND txt files
$files = glob(‘*.{php,txt}’, GLOB_BRACE);print_r($files);
/* output looks like:
Array
(
[0] => phptest.php
[1] => pi.php
[2] => post_output.php
[3] => test.php
[4] => log.txt
[5] => test.txt
)
*/請注意,這些檔案其實是可以返回一個路徑,這取決於查詢條件:$files = glob(‘../images/a*.jpg’);print_r($files);
/* output looks like:
Array
(
[0] => ../images/apple.jpg
[1] => ../images/art.jpg
)
*/如果你想獲得每個檔案的完整路徑,你可以調用 realpath() 函數:$files = glob(‘../images/a*.jpg’);// applies the function to each array element
$files = array_map(‘realpath’,$files);print_r($files);
/* output looks like:
Array
(
[0] => C:wampwwwimagesapple.jpg
[1] => C:wampwwwimagesart.jpg
)
*/3、記憶體使用量資訊通過偵測指令碼的記憶體使用量情況,有利於代碼的最佳化。PHP 提供了一個垃圾收集器和一個非常複雜的記憶體管理器。指令碼執行時所使用的記憶體量,有升有跌。為了得到當前的記憶體使用量情況,我們可以使用 memory_get_usage() 函數。如果需要獲得任意時間點的最高記憶體使用量量,則可以使用 memory_limit() 函數。echo “Initial: “.memory_get_usage().” bytes n”;
/* prints
Initial: 361400 bytes
*/// let’s use up some memory
for ($i = 0; $i < 100000; $i++) {
$array []= md5($i);
}// let’s remove half of the array
for ($i = 0; $i < 100000; $i++) {
unset($array[$i]);
}echo “Final: “.memory_get_usage().” bytes n”;
/* prints
Final: 885912 bytes
*/echo “Peak: “.memory_get_peak_usage().” bytes n”;
/* prints
Peak: 13687072 bytes
*/4、CPU 使用資訊為此,我們要利用 getrusage() 函數。請記住這個函數不適用於 Windows 平台。print_r(getrusage());
/* prints
Array
(
[ru_oublock] => 0
[ru_inblock] => 0
[ru_msgsnd] => 2
[ru_msgrcv] => 3
[ru_maxrss] => 12692
[ru_ixrss] => 764
[ru_idrss] => 3864
[ru_minflt] => 94
[ru_majflt] => 0
[ru_nsignals] => 1
[ru_nvcsw] => 67
[ru_nivcsw] => 4
[ru_nswap] => 0
[ru_utime.tv_usec] => 0
[ru_utime.tv_sec] => 0
[ru_stime.tv_usec] => 6269
[ru_stime.tv_sec] => 0
)*/這可能看起來有點神秘,除非你已經有系統管理員許可權。以下是每個值的具體說明(你不需要記住這些):ru_oublock: block output operations
ru_inblock: block input operations
ru_msgsnd: messages sent
ru_msgrcv: messages received
ru_maxrss: maximum resident set size
ru_ixrss: integral shared memory size
ru_idrss: integral unshared data size
ru_minflt: page reclaims
ru_majflt: page faults
ru_nsignals: signals received
ru_nvcsw: voluntary context switches
ru_nivcsw: involuntary context switches
ru_nswap: swaps
ru_utime.tv_usec: user time used (microseconds)
ru_utime.tv_sec: user time used (seconds)
ru_stime.tv_usec: system time used (microseconds)
ru_stime.tv_sec: system time used (seconds)要知道指令碼消耗多少 CPU 功率,我們需要看看 ‘user time’ 和’system time’ 兩個參數的值。秒和微秒部分預設是單獨提供的。你可以除以 100 萬微秒,並加上秒的參數值,得到一個十進位的總秒數。讓我們來看一個例子:// sleep for 3 seconds (non-busy)
sleep(3);$data = getrusage();
echo “User time: “.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo “System time: “.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);/* prints
User time: 0.011552
System time: 0
*/儘管指令碼運行用了大約 3 秒鐘,CPU 使用率卻非常非常低。因為在睡眠啟動並執行過程中,該指令碼實際上不消耗 CPU 資源。還有許多其他的任務,可能需要一段時間,但不佔用類似等待磁碟操作等 CPU 時間。因此正如你所看到的,CPU 使用率和已耗用時間的實際長度並不總是相同的。下面是一個例子:// loop 10 million times (busy)
for($i=0;$i<10000000;$i++) {}$data = getrusage();
echo “User time: “.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo “System time: “.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);/* prints
User time: 1.424592
System time: 0.004204
*/這花了大約 1.4 秒的 CPU 時間,但幾乎都是使用者時間,因為沒有系統調用。系統時間是指花費在執行程式的系統調用時的 CPU 開銷。下面是一個例子:$start = microtime(true);
// keep calling microtime for about 3 seconds
while(microtime(true) – $start < 3) {}$data = getrusage();
echo “User time: “.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo “System time: “.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);/* prints
User time: 1.088171
System time: 1.675315
*/現在我們有相當多的系統時間佔用。這是因為指令碼多次調用 microtime() 函數,該函數需要向作業系統發出請求,以擷取所需時間。你也可能會注意到已耗用時間加起來不到 3 秒。這是因為有可能在伺服器上同時存在其他進程,並且指令碼沒有 100% 使用 CPU 的整個 3 秒期間。5、魔術常量PHP 提供了擷取當前行號 (__LINE__)、檔案路徑 (__FILE__)、目錄路徑 (__DIR__)、函數名 (__FUNCTION__)、類名 (__CLASS__)、方法名 (__METHOD__) 和命名空間 (__NAMESPACE__) 等有用的魔術常量。在這篇文章中不作一一介紹,但是我將告訴你一些用例。當包含其他指令檔時,使用 __FILE__ 常量(或者使用 PHP5.3 新具有的 __DIR__ 常量):// this is relative to the loaded script’s path
// it may cause problems when running scripts from different directories
require_once(‘config/database.php’);// this is always relative to this file’s path
// no matter where it was included from
require_once(dirname(__FILE__) . ‘/config/database.php’);使用 __LINE__ 使得調試更為輕鬆。你可以跟蹤到具體行號。// some code
// …
my_debug(“some debug message”,__LINE__);
/* prints
Line 4: some debug message
*/// some more code
// …
my_debug(“another debug message”, __LINE__);
/* prints
Line 11: another debug message
*/function my_debug($msg, $line) {
echo “Line $line: $msgn”;
}6、產生唯一識別碼某些情境下,可能需要產生一個唯一的字串。我看到很多人使用 md5() 函數,即使它並不完全意味著這個目的:// generate unique string
echo md5(time() . mt_rand(1,1000000));There is actually a PHP function named uniqid() that ismeant to be used for this.// generate unique string
echo uniqid();
/* prints
4bd67c947233e
*/// generate another unique string
echo uniqid();
/* prints
4bd67c9472340
*/你可能會注意到,儘管字串是唯一的,前幾個字元卻是類似的,這是因為產生的字串與伺服器時間相關。但實際上也存在友好的一方面,由於每個新產生的 ID 會按字母順序排列,這樣排序就變得很簡單。為了減少重複的機率,你可以傳遞一個首碼,或第二個參數來增加熵:// with prefix
echo uniqid(‘foo_’);
/* prints
foo_4bd67d6cd8b8f
*/// with more entropy
echo uniqid(”,true);
/* prints
4bd67d6cd8b926.12135106
*/// both
echo uniqid(‘bar_’,true);
/* prints
bar_4bd67da367b650.43684647
*/這個函數將產生比 md5() 更短的字串,能節省一些空間。7、序列化你有沒有遇到過需要在資料庫或文字檔儲存一個複雜變數的情況?你可能沒能想出一個格式化字串並轉換成數組或對象的好方法,PHP 已經為你準備好此功能。有兩種序列化變數的流行方法。下面是一個例子,使用 serialize() 和 unserialize() 函數:// a complex array
$myvar = array(
‘hello’,
42,
array(1,’two’),
‘apple’
);// convert to a string
$string = serialize($myvar);echo $string;
/* prints
a:4:{i:0;s:5:”hello”;i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:”two”;}i:3;s:5:”apple”;}
*/// you can reproduce the original variable
$newvar = unserialize($string);print_r($newvar);
/* prints
Array
(
[0] => hello
[1] => 42
[2] => Array
(
[0] => 1
[1] => two
)[3] => apple
)
*/這是原生的 PHP 序列化方法。然而,由於 JSON 近年來大受歡迎,PHP5.2 中已經加入了對 JSON 格式的支援。現在你可以使用 json_encode() 和 json_decode() 函數:// a complex array
$myvar = array(
‘hello’,
42,
array(1,’two’),
‘apple’
);// convert to a string
$string = json_encode($myvar);echo $string;
/* prints
["hello",42,[1,"two"],”apple”]
*/// you can reproduce the original variable
$newvar = json_decode($string);print_r($newvar);
/* prints
Array
(
[0] => hello
[1] => 42
[2] => Array
(
[0] => 1
[1] => two
)[3] => apple
)
*/這將更為行之有效,尤其與 JavaScript 等許多其他語言相容。然而對於複雜的對象,某些資訊可能會丟失。8、壓縮字串在談到壓縮時,我們通常想到檔案壓縮,如 ZIP 壓縮等。在 PHP 中字串壓縮也是可能的,但不涉及任何壓縮檔。在下面的例子中,我們要利用 gzcompress() 和 gzuncompress() 函數:$string =
“Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Nunc ut elit id mi ultricies
adipiscing. Nulla facilisi. Praesent pulvinar,
sapien vel feugiat vestibulum, nulla dui pretium orci,
non ultricies elit lacus quis ante. Lorem ipsum dolor
sit amet, consectetur adipiscing elit. Aliquam
pretium ullamcorper urna quis iaculis. Etiam ac massa
sed turpis tempor luctus. Curabitur sed nibh eu elit
mollis congue. Praesent ipsum diam, consectetur vitae
ornare a, aliquam a nunc. In id magna pellentesque
tellus posuere adipiscing. Sed non mi metus, at lacinia
augue. Sed magna nisi, ornare in mollis in, mollis
sed nunc. Etiam at justo in leo congue mollis.
Nullam in neque eget metus hendrerit scelerisque
eu non enim. Ut malesuada lacus eu nulla bibendum
id euismod urna sodales. “;$compressed = gzcompress($string);echo “Original size: “. strlen($string).”n”;
/* prints
Original size: 800
*/echo “Compressed size: “. strlen($compressed).”n”;
/* prints
Compressed size: 418
*/// getting it back
$original = gzuncompress($compressed);這種操作的壓縮率能達到 50% 左右。另外的函數 gzencode() 和 gzdecode() 能達到類似結果,通過使用不同的壓縮演算法。9、註冊停止功能有一個函數叫做 register_shutdown_function(),可以讓你在某段指令碼完成運行之前,執行一些指定代碼。假設你需要在指令碼執行結束前捕獲一些基準統計資訊,例如啟動並執行時間長度:// capture the start time
$start_time = microtime(true);// do some stuff
// …// display how long the script took
echo “execution took: “.
(microtime(true) – $start_time).
” seconds.”;這似乎微不足道,你只需要在指令碼啟動並執行最後添加相關代碼。但是如果你調用過 exit() 函數,該代碼將無法運行。此外,如果有一個致命的錯誤,或者指令碼被使用者意外終止,它可能無法再次運行。當你使用 register_shutdown_function() 函數,代碼將繼續執行,不論指令碼是否停止運行:$start_time = microtime(true);register_shutdown_function(‘my_shutdown’);// do some stuff
// …function my_shutdown() {
global $start_time;echo “execution took: “.
(microtime(true) – $start_time).
” seconds.”;
} 喜歡我的文章,請關注我的toceansoft
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.