自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

那些未曾了解的PHP函數(shù)和功能

開發(fā) 后端 前端
PHP真正的強大便是它的函數(shù),文章將介紹一些實用的PHP函數(shù)和它的功能,這些函數(shù)并沒有得到我們充分的利用,但它們的功能是非常強大的。

PHP的真正威力源自于它的函數(shù),但有些PHP函數(shù)并沒有得到充分的利用,也并不是所有人都會從頭到尾一頁一頁地閱讀手冊和函數(shù)參考,這里將向您介紹這些實用的函數(shù)和功能。

51CTO推薦專題: PHP開發(fā)基礎(chǔ)入門

1、任意參數(shù)數(shù)目的函數(shù)

你可能已經(jīng)知道,PHP允許定義可選參數(shù)的函數(shù)。但也有完全允許任意數(shù)目的函數(shù)參數(shù)的方法。以下是可選參數(shù)的例子:

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //functionwith2optionalarguments  
  4. functionfoo($arg1=”,$arg2=”){  
  5.  
  6. echo“arg1:$arg1\n”;  
  7. echo“arg2:$arg2\n”;  
  8.  
  9. }  
  10.  
  11. foo(‘hello’,'world’);  
  12. /*prints:  
  13. arg1:hello  
  14. arg2:world  
  15. */  
  16.  
  17. foo();  
  18. /*prints:  
  19. arg1:  
  20. arg2:  
  21. */ 

現(xiàn)在讓我們看看如何建立能夠接受任何參數(shù)數(shù)目的函數(shù)。這一次需要使用func_get_args()函數(shù):

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //yes,theargumentlistcanbeempty  
  4. functionfoo(){  
  5.  
  6. //returnsanarrayofallpassedarguments  
  7. $args=func_get_args();  
  8.  
  9. foreach($argsas$k=>$v){  
  10. echo“arg”.($k+1).”:$v\n”;  
  11. }  
  12.  
  13. }  
  14.  
  15. foo();  
  16. /*printsnothing*/  
  17.  
  18. foo(‘hello’);  
  19. /*prints  
  20. arg1:hello  
  21. */  
  22.  
  23. foo(‘hello’,‘world’,‘again’);  
  24. /*prints  
  25. arg1:hello  
  26. arg2:world  
  27. arg3:again  
  28. */ 

#p#

2、使用Glob()查找文件

許多PHP函數(shù)具有長描述性的名稱。然而可能會很難說出glob()函數(shù)能做的事情,除非你已經(jīng)通過多次使用并熟悉了它??梢园阉醋魇潜萻candir()函數(shù)更強大的版本,可以按照某種模式搜索文件。

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //getallphpfiles  
  4. $files=glob(‘*.php’);  
  5.  
  6. print_r($files);  
  7. /*outputlookslike:  
  8. Array  
  9. (  
  10. [0]=>phptest.php  
  11. [1]=>pi.php  
  12. [2]=>post_output.php  
  13. [3]=>test.php  
  14. )  
  15. */ 

你可以像這樣獲得多個文件:

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //getallphpfilesANDtxtfiles  
  4. $files=glob(‘*.{php,txt}’,GLOB_BRACE);  
  5.  
  6. print_r($files);  
  7. /*outputlookslike:  
  8. Array  
  9. (  
  10. [0]=>phptest.php  
  11. [1]=>pi.php  
  12. [2]=>post_output.php  
  13. [3]=>test.php  
  14. [4]=>log.txt  
  15. [5]=>test.txt  
  16. )  
  17. */ 

請注意,這些文件其實是可以返回一個路徑,這取決于查詢條件:

  1. 以下為引用的內(nèi)容:  
  2.  
  3. $files=glob(‘../images/a*.jpg’);  
  4.  
  5. print_r($files);  
  6. /*outputlookslike:  
  7. Array  
  8. (  
  9. [0]=>../images/apple.jpg  
  10. [1]=>../images/art.jpg  
  11. )  
  12. */ 

如果你想獲得每個文件的完整路徑,你可以調(diào)用realpath()函數(shù):

  1. 以下為引用的內(nèi)容:  
  2.  
  3. $files=glob(‘../images/a*.jpg’);  
  4.  
  5. //appliesthefunctiontoeacharrayelement  
  6. $files=array_map(‘realpath’,$files);  
  7.  
  8. print_r($files);  
  9. /*outputlookslike:  
  10. Array  
  11. (  
  12. [0]=>C:\wamp\www\images\apple.jpg  
  13. [1]=>C:\wamp\www\images\art.jpg  
  14. )  
  15. */ 

 #p#

3、內(nèi)存使用信息

通過偵測腳本的內(nèi)存使用情況,有利于代碼的優(yōu)化。PHP提供了一個垃圾收集器和一個非常復(fù)雜的內(nèi)存管理器。腳本執(zhí)行時所使用的內(nèi)存量,有升有跌。為了得到當(dāng)前的內(nèi)存使用情況,我們可以使用memory_get_usage()函數(shù)。如果需要獲得任意時間點的***內(nèi)存使用量,則可以使用memory_limit()函數(shù)。

  1. 以下為引用的內(nèi)容:  
  2.  
  3. echo“Initial:“.memory_get_usage().”bytes\n”;  
  4. /*prints  
  5. Initial:361400bytes  
  6. */  
  7.  
  8. //let’suseupsomememory  
  9. for($i=0;$i<100000;$i++){  
  10. $array[]=md5($i);  
  11. }  
  12.  
  13. //let'sremovehalfofthearray  
  14. for($i=0;$i<100000;$i++){  
  15. unset($array[$i]);  
  16. }  
  17.  
  18. echo"Final:".memory_get_usage()."bytes\n";  
  19. /*prints  
  20. Final:885912bytes  
  21. */  
  22.  
  23. echo"Peak:".memory_get_peak_usage()."bytes\n";  
  24. /*prints  
  25. Peak:13687072bytes  
  26. */ 

4、CPU使用信息

為此,我們要利用getrusage()函數(shù)。請記住這個函數(shù)不適用于Windows平臺。

  1. 以下為引用的內(nèi)容:  
  2.  
  3. print_r(getrusage());  
  4. /*prints  
  5. Array  
  6. (  
  7. [ru_oublock]=>0  
  8. [ru_inblock]=>0  
  9. [ru_msgsnd]=>2  
  10. [ru_msgrcv]=>3  
  11. [ru_maxrss]=>12692  
  12. [ru_ixrss]=>764  
  13. [ru_idrss]=>3864  
  14. [ru_minflt]=>94  
  15. [ru_majflt]=>0  
  16. [ru_nsignals]=>1  
  17. [ru_nvcsw]=>67  
  18. [ru_nivcsw]=>4  
  19. [ru_nswap]=>0  
  20. [ru_utime.tv_usec]=>0  
  21. [ru_utime.tv_sec]=>0  
  22. [ru_stime.tv_usec]=>6269  
  23. [ru_stime.tv_sec]=>0  

這可能看起來有點神秘,除非你已經(jīng)有系統(tǒng)管理員權(quán)限。以下是每個值的具體說明(你不需要記住這些):

  1. 以下為引用的內(nèi)容:  
  2.  
  3. ru_oublock:blockoutputoperations  
  4. ru_inblock:blockinputoperations  
  5. ru_msgsnd:messagessent  
  6. ru_msgrcv:messagesreceived  
  7. ru_maxrss:maximumresidentsetsize  
  8. ru_ixrss:integralsharedmemorysize  
  9. ru_idrss:integralunshareddatasize  
  10. ru_minflt:pagereclaims  
  11. ru_majflt:pagefaults  
  12. ru_nsignals:signalsreceived  
  13. ru_nvcsw:voluntarycontextswitches  
  14. ru_nivcsw:involuntarycontextswitches  
  15. ru_nswap:swaps  
  16. ru_utime.tv_usec:usertimeused(microseconds)  
  17. ru_utime.tv_sec:usertimeused(seconds)  
  18. ru_stime.tv_usec:systemtimeused(microseconds)  
  19. ru_stime.tv_sec:systemtimeused(seconds) 

 

要知道腳本消耗多少CPU功率,我們需要看看‘usertime’和’systemtime’兩個參數(shù)的值。秒和微秒部分默認(rèn)是單獨提供的。你可以除以100萬微秒,并加上秒的參數(shù)值,得到一個十進制的總秒數(shù)。讓我們來看一個例子:

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //sleepfor3seconds(non-busy)  
  4. sleep(3);  
  5.  
  6. $data=getrusage();  
  7. echo“Usertime:“.  
  8. ($data['ru_utime.tv_sec']+  
  9. $data['ru_utime.tv_usec']/1000000);  
  10. echo“Systemtime:“.  
  11. ($data['ru_stime.tv_sec']+  
  12. $data['ru_stime.tv_usec']/1000000);  
  13.  
  14. /*prints  
  15. Usertime:0.011552  
  16. Systemtime:0  
  17. */ 

盡管腳本運行用了大約3秒鐘,CPU使用率卻非常非常低。因為在睡眠運行的過程中,該腳本實際上不消耗CPU資源。還有許多其他的任務(wù),可能需要一段時間,但不占用類似等待磁盤操作等CPU時間。因此正如你所看到的,CPU使用率和運行時間的實際長度并不總是相同的。下面是一個例子:

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //loop10milliontimes(busy)  
  4. for($i=0;$i<10000000;$i++){  
  5.  
  6. }  
  7.  
  8. $data=getrusage();  
  9. echo"Usertime:".  
  10. ($data['ru_utime.tv_sec']+  
  11. $data['ru_utime.tv_usec']/1000000);  
  12. echo"Systemtime:".  
  13. ($data['ru_stime.tv_sec']+  
  14. $data['ru_stime.tv_usec']/1000000);  
  15.  
  16. /*prints  
  17. Usertime:1.424592  
  18. Systemtime:0.004204  
  19. */ 

 

這花了大約1.4秒的CPU時間,但幾乎都是用戶時間,因為沒有系統(tǒng)調(diào)用。系統(tǒng)時間是指花費在執(zhí)行程序的系統(tǒng)調(diào)用時的CPU開銷。下面是一個例子:

  1. 以下為引用的內(nèi)容:  
  2.  
  3. $start=microtime(true);  
  4. //keepcallingmicrotimeforabout3seconds  
  5. while(microtime(true)-$start<3){  
  6.  
  7. }  
  8.  
  9. $data=getrusage();  
  10. echo"Usertime:".  
  11. ($data['ru_utime.tv_sec']+  
  12. $data['ru_utime.tv_usec']/1000000);  
  13. echo"Systemtime:".  
  14. ($data['ru_stime.tv_sec']+  
  15. $data['ru_stime.tv_usec']/1000000);  
  16.  
  17. /*prints  
  18. Usertime:1.088171  
  19. Systemtime:1.675315  
  20. */ 

現(xiàn)在我們有相當(dāng)多的系統(tǒng)時間占用。這是因為腳本多次調(diào)用microtime()函數(shù),該函數(shù)需要向操作系統(tǒng)發(fā)出請求,以獲取所需時間。你也可能會注意到運行時間加起來不到3秒。這是因為有可能在服務(wù)器上同時存在其他進程,并且腳本沒有100%使用CPU的整個3秒持續(xù)時間。

 #p#

5、魔術(shù)常量

PHP提供了獲取當(dāng)前行號(__LINE__)、文件路徑(__FILE__)、目錄路徑(__DIR__)、函數(shù)名(__FUNCTION__)、類名(__CLASS__)、方法名(__METHOD__)和命名空間(__NAMESPACE__)等有用的魔術(shù)常量。在這篇文章中不作一一介紹,但是我將告訴你一些用例。當(dāng)包含其他腳本文件時,使用__FILE__常量(或者使用PHP5.3新具有的__DIR__常量):

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //thisisrelativetotheloadedscript'spath  
  4. //itmaycauseproblemswhenrunningscriptsfromdifferentdirectories  
  5. require_once('config/database.php');  
  6.  
  7. //thisisalwaysrelativetothisfile'spath  
  8. //nomatterwhereitwasincludedfrom  
  9. require_once(dirname(__FILE__).'/config/database.php'); 

 

使用__LINE__使得調(diào)試更為輕松。你可以跟蹤到具體行號。

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //somecode  
  4. //...  
  5. my_debug("somedebugmessage",__LINE__);  
  6. /*prints  
  7. Line4:somedebugmessage  
  8. */  
  9.  
  10. //somemorecode  
  11. //...  
  12. my_debug("anotherdebugmessage",__LINE__);  
  13. /*prints  
  14. Line11:anotherdebugmessage  
  15. */  
  16.  
  17. functionmy_debug($msg,$line){  
  18. echo"Line$line:$msg 

6、生成唯一標(biāo)識符

某些場景下,可能需要生成一個唯一的字符串。我看到很多人使用md5()函數(shù),即使它并不完全意味著這個目的:

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //generateuniquestring  
  4. echomd5(time().mt_rand(1,1000000));  
  5. ThereisactuallyaPHPfunctionnameduniqid()thatismeanttobeusedforthis.  
  6.  
  7. //generateuniquestring  
  8. echouniqid();  
  9. /*prints  
  10. 4bd67c947233e  
  11. */  
  12.  
  13. //generateanotheruniquestring  
  14. echouniqid();  
  15. /*prints  
  16. 4bd67c9472340  
  17. */ 

你可能會注意到,盡管字符串是唯一的,前幾個字符卻是類似的,這是因為生成的字符串與服務(wù)器時間相關(guān)。但實際上也存在友好的一方面,由于每個新生成的ID會按字母順序排列,這樣排序就變得很簡單。為了減少重復(fù)的概率,你可以傳遞一個前綴,或第二個參數(shù)來增加熵。

  1. 以下為引用的內(nèi)容:  
  2.  
  3. //withprefix  
  4. echouniqid('foo_');  
  5. /*prints  
  6. foo_4bd67d6cd8b8f  
  7. */  
  8.  
  9. //withmoreentropy  
  10. echouniqid('',true);  
  11. /*prints  
  12. 4bd67d6cd8b926.12135106  
  13. */  
  14.  
  15. //both  
  16. echouniqid('bar_',true);  
  17. /*prints  
  18. bar_4bd67da367b650.43684647  
  19. */ 

這個函數(shù)將產(chǎn)生比md5()更短的字符串,能節(jié)省一些空間。

7、序列化

你有沒有遇到過需要在數(shù)據(jù)庫或文本文件存儲一個復(fù)雜變量的情況?你可能沒能想出一個格式化字符串并轉(zhuǎn)換成數(shù)組或?qū)ο蟮暮梅椒?,PHP已經(jīng)為你準(zhǔn)備好此功能。有兩種序列化變量的流行方法。下面是一個例子,使用serialize()和unserialize()函數(shù)。以下為引用的內(nèi)容:

  1. //acomplexarray  
  2. $myvar=array(  
  3. 'hello',  
  4. 42,  
  5. array(1,'two'),  
  6. 'apple'  
  7. );  
  8.  
  9. //converttoastring  
  10. $string=serialize($myvar);  
  11.  
  12. echo$string;  
  13. /*prints  
  14. 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";}  
  15. */  
  16.  
  17. //youcanreproducetheoriginalvariable  
  18. $newvar=unserialize($string);  
  19.  
  20. print_r($newvar);  
  21. /*prints  
  22. Array  
  23. (  
  24. [0]=>hello  
  25. [1]=>42  
  26. [2]=>Array  
  27. (  
  28. [0]=>1  
  29. [1]=>two  
  30. )  
  31.  
  32. [3]=>apple  
  33. )  
  34. */ 

這是原生的PHP序列化方法。然而,由于JSON近年來大受歡迎,PHP5.2中已經(jīng)加入了對JSON格式的支持?,F(xiàn)在你可以使用json_encode()和json_decode()函數(shù),以下為引用的內(nèi)容:

  1. //acomplexarray  
  2. $myvar=array(  
  3. ‘hello’,  
  4. 42,  
  5. array(1,’two’),  
  6. ‘apple’  
  7. );  
  8.  
  9. //converttoastring  
  10. $string=json_encode($myvar);  
  11.  
  12. echo$string;  
  13. /*prints  
  14. ["hello",42,[1,"two"],”apple”]  
  15. */  
  16.  
  17. //youcanreproducetheoriginalvariable  
  18. $newvar=json_decode($string);  
  19.  
  20. print_r($newvar);  
  21. /*prints  
  22. Array  
  23. (  
  24. [0]=>hello  
  25. [1]=>42  
  26. [2]=>Array  
  27. (  
  28. [0]=>1  
  29. [1]=>two  
  30. )  
  31.  
  32. [3]=>apple  
  33. )  
  34. */ 

這將更為行之有效,尤其與JavaScript等許多其他語言兼容。然而對于復(fù)雜的對象,某些信息可能會丟失。

文章轉(zhuǎn)自PHP之家,

原文地址:http://www.phpzj.org/php_0002.html

【編輯推薦】

  1. PHP開發(fā)者不可不知的五件事
  2. PHP開發(fā)人員容易忽略的幾點精華
  3. FirePHP:像Firebug那樣調(diào)試你的PHP代碼
  4. PHP應(yīng)用JSON技巧講解
  5. 解讀PHP冒泡排序技巧

 

責(zé)任編輯:王曉東 來源: PHP之家
相關(guān)推薦

2013-10-21 17:57:54

2009-11-30 14:27:42

2009-12-01 15:14:32

PHP Substr庫

2009-11-25 14:06:53

PHP函數(shù)arsort

2009-12-03 15:23:48

PHP建立和關(guān)閉數(shù)據(jù)庫

2014-08-26 09:52:57

2023-06-27 17:02:05

PHP功能

2009-11-30 15:10:46

PHP substr函

2015-04-13 15:41:53

SAPF1

2010-07-27 11:29:43

Flex

2009-11-26 13:50:11

PHP函數(shù)str_re

2009-11-25 17:48:18

PHP文件系統(tǒng)相關(guān)函數(shù)

2015-03-20 13:20:11

PHP框架全方面了解PHP

2009-12-04 09:50:59

PHP ob_star

2009-11-30 17:49:51

PHP函數(shù)preg_s

2010-09-02 15:45:18

PHP函數(shù)echo

2019-07-02 11:01:35

SpringBean配置

2009-12-03 15:40:41

PHP獲取數(shù)據(jù)庫表信息

2009-11-26 14:38:08

PHP函數(shù)echo()

2009-11-26 13:52:07

PHP字符串替換函數(shù)s
點贊
收藏

51CTO技術(shù)棧公眾號