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

九個(gè)PHP很有用的功能

開發(fā) 后端 前端
下面是九個(gè)PHP中很有用的功能,不知道你用過了嗎?

下面是九個(gè)PHP中很有用的功能,不知道你用過了嗎?

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

你可能知道PHP允許你定義一個(gè)默認(rèn)參數(shù)的函數(shù)。但你可能并不知道PHP還允許你定義一個(gè)完全任意的參數(shù)的函數(shù)

下面是一個(gè)示例向你展示了默認(rèn)參數(shù)的函數(shù):

  1. // 兩個(gè)默認(rèn)參數(shù)的函數(shù) 
  2. function foo($arg1 = ''$arg2 = '') { 
  3.   
  4.     echo "arg1: $arg1\n"
  5.     echo "arg2: $arg2\n"
  6.   
  7.   
  8. foo('hello','world'); 
  9. /* 輸出: 
  10. arg1: hello 
  11. arg2: world 
  12. */ 
  13.   
  14. foo(); 
  15. /* 輸出: 
  16. arg1: 
  17. arg2: 

現(xiàn)在我們來看一看一個(gè)不定參數(shù)的函數(shù),其使用到了?func_get_args()方法:

  1. // 是的,形參列表為空 
  2. function foo() { 
  3.   
  4.     // 取得所有的傳入?yún)?shù)的數(shù)組 
  5.     $args = func_get_args(); 
  6.   
  7.     foreach ($args as $k => $v) { 
  8.         echo "arg".($k+1).": $v\n"
  9.     } 
  10.   
  11.   
  12. foo(); 
  13. /* 什么也不會(huì)輸出 */ 
  14.   
  15. foo('hello'); 
  16. /* 輸出 
  17. arg1: hello 
  18. */ 
  19.   
  20. foo('hello''world''again'); 
  21. /* 輸出 
  22. arg1: hello 
  23. arg2: world 
  24. arg3: again 
  25. */ 

2. 使用 Glob() 查找文件

很多PHP的函數(shù)都有一個(gè)比較長的自解釋的函數(shù)名,但是,當(dāng)你看到?glob() 的時(shí)候,你可能并不知道這個(gè)函數(shù)是用來干什么的,除非你對(duì)它已經(jīng)很熟悉了。

你可以認(rèn)為這個(gè)函數(shù)就好?scandir() 一樣,其可以用來查找文件。

  1. // 取得所有的后綴為PHP的文件 
  2. $files = glob('*.php'); 
  3.   
  4. print_r($files); 
  5. /* 輸出: 
  6. Array 
  7. ( 
  8.     [0] => phptest.php 
  9.     [1] => pi.php 
  10.     [2] => post_output.php 
  11.     [3] => test.php 
  12. ) 
  13. */ 

你還可以查找多種后綴名

  1. // 取PHP文件和TXT文件 
  2. $files = glob('*.{php,txt}', GLOB_BRACE); 
  3.   
  4. print_r($files); 
  5. /* 輸出: 
  6. Array 
  7. ( 
  8.     [0] => phptest.php 
  9.     [1] => pi.php 
  10.     [2] => post_output.php 
  11.     [3] => test.php 
  12.     [4] => log.txt 
  13.     [5] => test.txt 
  14. ) 
  15. */ 

你還可以加上路徑:

  1. $files = glob('../images/a*.jpg'); 
  2.   
  3. print_r($files); 
  4. /* 輸出: 
  5. Array 
  6. ( 
  7.     [0] => ../images/apple.jpg 
  8.     [1] => ../images/art.jpg 
  9. ) 
  10. */ 

如果你想得到絕對(duì)路徑,你可以調(diào)用?realpath() 函數(shù):

  1. $files = glob('../images/a*.jpg'); 
  2.   
  3. // applies the function to each array element 
  4. $files = array_map('realpath',$files); 
  5.   
  6. print_r($files); 
  7. /* output looks like: 
  8. Array 
  9. ( 
  10.     [0] => C:\wamp\www\images\apple.jpg 
  11.     [1] => C:\wamp\www\images\art.jpg 
  12. ) 
  13. */ 

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

觀察你程序的內(nèi)存使用能夠讓你更好的優(yōu)化你的代碼。

PHP 是有垃圾回收機(jī)制的,而且有一套很復(fù)雜的內(nèi)存管理機(jī)制。你可以知道你的腳本所使用的內(nèi)存情況。要知道當(dāng)前內(nèi)存使用情況,你可以使用? memory_get_usage() 函數(shù),如果你想知道使用內(nèi)存的峰值,你可以調(diào)用memory_get_peak_usage() 函數(shù)。

  1. echo "Initial: ".memory_get_usage()." bytes \n"
  2. /* 輸出 
  3. Initial: 361400 bytes 
  4. */ 
  5.   
  6. // 使用內(nèi)存 
  7. for ($i = 0; $i < 100000; $i++) { 
  8.     $array []= md5($i); 
  9.   
  10. // 刪除一半的內(nèi)存 
  11. for ($i = 0; $i < 100000; $i++) { 
  12.     unset($array[$i]); 
  13.   
  14. echo "Final: ".memory_get_usage()." bytes \n"
  15. /* prints 
  16. Final: 885912 bytes 
  17. */ 
  18.   
  19. echo "Peak: ".memory_get_peak_usage()." bytes \n"
  20. /* 輸出峰值 
  21. Peak: 13687072 bytes 
  22. */ 

#p#

4. CPU使用信息

使用?getrusage() 函數(shù)可以讓你知道CPU的使用情況。注意,這個(gè)功能在Windows下不可用。

  1. print_r(getrusage()); 
  2. /* 輸出 
  3. Array 
  4. ( 
  5.     [ru_oublock] => 0 
  6.     [ru_inblock] => 0 
  7.     [ru_msgsnd] => 2 
  8.     [ru_msgrcv] => 3 
  9.     [ru_maxrss] => 12692 
  10.     [ru_ixrss] => 764 
  11.     [ru_idrss] => 3864 
  12.     [ru_minflt] => 94 
  13.     [ru_majflt] => 0 
  14.     [ru_nsignals] => 1 
  15.     [ru_nvcsw] => 67 
  16.     [ru_nivcsw] => 4 
  17.     [ru_nswap] => 0 
  18.     [ru_utime.tv_usec] => 0 
  19.     [ru_utime.tv_sec] => 0 
  20.     [ru_stime.tv_usec] => 6269 
  21.     [ru_stime.tv_sec] => 0 
  22. ) 
  23.   
  24. */ 

這個(gè)結(jié)構(gòu)看上出很晦澀,除非你對(duì)CPU很了解。下面一些解釋:

  • ru_oublock: 塊輸出操作
  • ru_inblock: 塊輸入操作
  • ru_msgsnd: 發(fā)送的message
  • ru_msgrcv: 收到的message
  • ru_maxrss: 最大駐留集大小
  • ru_ixrss: 全部共享內(nèi)存大小
  • ru_idrss:全部非共享內(nèi)存大小
  • ru_minflt: 頁回收
  • ru_majflt: 頁失效
  • ru_nsignals: 收到的信號(hào)
  • ru_nvcsw: 主動(dòng)上下文切換
  • ru_nivcsw: 被動(dòng)上下文切換
  • ru_nswap: 交換區(qū)
  • ru_utime.tv_usec: 用戶態(tài)時(shí)間 (microseconds)
  • ru_utime.tv_sec: 用戶態(tài)時(shí)間(seconds)
  • ru_stime.tv_usec: 系統(tǒng)內(nèi)核時(shí)間 (microseconds)
  • ru_stime.tv_sec: 系統(tǒng)內(nèi)核時(shí)間?(seconds)

要看到你的腳本消耗了多少CPU,我們需要看看“用戶態(tài)的時(shí)間”和“系統(tǒng)內(nèi)核時(shí)間”的值。秒和微秒部分是分別提供的,您可以把微秒值除以100萬,并把它添加到秒的值后,可以得到有小數(shù)部分的秒數(shù)。

  1. // sleep for 3 seconds (non-busy) 
  2. sleep(3); 
  3.   
  4. $data = getrusage(); 
  5. echo "User time: "
  6.     ($data['ru_utime.tv_sec'] + 
  7.     $data['ru_utime.tv_usec'] / 1000000); 
  8. echo "System time: "
  9.     ($data['ru_stime.tv_sec'] + 
  10.     $data['ru_stime.tv_usec'] / 1000000); 
  11.   
  12. /* 輸出 
  13. User time: 0.011552 
  14. System time: 0 
  15. */ 

sleep是不占用系統(tǒng)時(shí)間的,我們可以來看下面的一個(gè)例子:

  1. // loop 10 million times (busy) 
  2. for($i=0;$i<10000000;$i++) { 
  3.   
  4.   
  5. $data = getrusage(); 
  6. echo "User time: "
  7.     ($data['ru_utime.tv_sec'] + 
  8.     $data['ru_utime.tv_usec'] / 1000000); 
  9. echo "System time: "
  10.     ($data['ru_stime.tv_sec'] + 
  11.     $data['ru_stime.tv_usec'] / 1000000); 
  12.   
  13. /* 輸出 
  14. User time: 1.424592 
  15. System time: 0.004204 
  16. */ 

這花了大約14秒的CPU時(shí)間,幾乎所有的都是用戶的時(shí)間,因?yàn)闆]有系統(tǒng)調(diào)用。

系統(tǒng)時(shí)間是CPU花費(fèi)在系統(tǒng)調(diào)用上的上執(zhí)行內(nèi)核指令的時(shí)間。下面是一個(gè)例子:

  1. $start = microtime(true); 
  2. // keep calling microtime for about 3 seconds 
  3. while(microtime(true) - $start < 3) { 
  4.   
  5.   
  6. $data = getrusage(); 
  7. echo "User time: "
  8.     ($data['ru_utime.tv_sec'] + 
  9.     $data['ru_utime.tv_usec'] / 1000000); 
  10. echo "System time: "
  11.     ($data['ru_stime.tv_sec'] + 
  12.     $data['ru_stime.tv_usec'] / 1000000); 
  13.   
  14. /* prints 
  15. User time: 1.088171 
  16. System time: 1.675315 
  17. */ 

我們可以看到上面這個(gè)例子更耗CPU。

5. 系統(tǒng)常量

PHP 提供非常有用的系統(tǒng)常量 可以讓你得到當(dāng)前的行號(hào) (__LINE__),文件 (__FILE__),目錄 (__DIR__),函數(shù)名 (__FUNCTION__),類名(__CLASS__),方法名(__METHOD__) 和名字空間 (__NAMESPACE__),很像C語言。

我們可以以為這些東西主要是用于調(diào)試,當(dāng)也不一定,比如我們可以在include其它文件的時(shí)候使用?__FILE__ (當(dāng)然,你也可以在 PHP 5.3以后使用 __DIR__ ),下面是一個(gè)例子。

  1. // this is relative to the loaded script's path 
  2. // it may cause problems when running scripts from different directories 
  3. require_once('config/database.php'); 
  4.   
  5. // this is always relative to this file's path 
  6. // no matter where it was included from 
  7. require_once(dirname(__FILE__) . '/config/database.php'); 

下面是使用 __LINE__ 來輸出一些debug的信息,這樣有助于你調(diào)試程序:

  1. // some code 
  2. // ... 
  3. my_debug("some debug message"__LINE__); 
  4. /* 輸出 
  5. Line 4: some debug message 
  6. */ 
  7.   
  8. // some more code 
  9. // ... 
  10. my_debug("another debug message"__LINE__); 
  11. /* 輸出 
  12. Line 11: another debug message 
  13. */ 
  14.   
  15. function my_debug($msg$line) { 
  16.     echo "Line $line: $msg\n"

#p#

6.生成唯一的ID

有很多人使用 md5() 來生成一個(gè)唯一的ID,如下所示:

  1. // generate unique string 
  2. echo md5(time() . mt_rand(1,1000000)); 

其實(shí),PHP中有一個(gè)叫?uniqid() 的函數(shù)是專門用來干這個(gè)的:

  1. // generate unique string 
  2. echo uniqid(); 
  3. /* 輸出 
  4. 4bd67c947233e 
  5. */ 
  6.   
  7. // generate another unique string 
  8. echo uniqid(); 
  9. /* 輸出 
  10. 4bd67c9472340 
  11. */ 

可能你會(huì)注意到生成出來的ID前幾位是一樣的,這是因?yàn)樯善饕蕾囉谙到y(tǒng)的時(shí)間,這其實(shí)是一個(gè)非常不錯(cuò)的功能,因?yàn)槟闶呛苋菀诪槟愕倪@些ID排序的。這點(diǎn)MD5是做不到的。

你還可以加上前綴避免重名:

  1. // 前綴 
  2. echo uniqid('foo_'); 
  3. /* 輸出 
  4. foo_4bd67d6cd8b8f 
  5. */ 
  6.   
  7. // 有更多的熵 
  8. echo uniqid('',true); 
  9. /* 輸出 
  10. 4bd67d6cd8b926.12135106 
  11. */ 
  12.   
  13. // 都有 
  14. echo uniqid('bar_',true); 
  15. /* 輸出 
  16. bar_4bd67da367b650.43684647 
  17. */ 

而且,生成出來的ID會(huì)比MD5生成的要短,這會(huì)讓你節(jié)省很多空間。

7. 序列化

你是否會(huì)把一個(gè)比較復(fù)雜的數(shù)據(jù)結(jié)構(gòu)存到數(shù)據(jù)庫或是文件中?你并不需要自己去寫自己的算法。PHP早已為你做好了,其提供了兩個(gè)函數(shù):?serialize() 和 unserialize():

  1. // 一個(gè)復(fù)雜的數(shù)組 
  2. $myvar = array
  3.     'hello'
  4.     42, 
  5.     array(1,'two'), 
  6.     'apple' 
  7. ); 
  8.   
  9. // 序列化 
  10. $string = serialize($myvar); 
  11.   
  12. echo $string
  13. /* 輸出 
  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. // 反序例化 
  18. $newvar = unserialize($string); 
  19.   
  20. print_r($newvar); 
  21. /* 輸出 
  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的原生函數(shù),然而在今天JSON越來越流行,所以在PHP5.2以后,PHP開始支持JSON,你可以使用 json_encode() 和 json_decode() 函數(shù)

  1. // a complex array 
  2. $myvar = array
  3.     'hello'
  4.     42, 
  5.     array(1,'two'), 
  6.     'apple' 
  7. ); 
  8.   
  9. // convert to a string 
  10. $string = json_encode($myvar); 
  11.   
  12. echo $string
  13. /* prints 
  14. ["hello",42,[1,"two"],"apple"] 
  15. */ 
  16.   
  17. // you can reproduce the original variable 
  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和其它語言。但是對(duì)于一些非常復(fù)雜的數(shù)據(jù)結(jié)構(gòu),可能會(huì)造成數(shù)據(jù)丟失。

8. 字符串壓縮

當(dāng)我們說到壓縮,我們可能會(huì)想到文件壓縮,其實(shí),字符串也是可以壓縮的。PHP提供了?gzcompress()gzuncompress() 函數(shù):

  1. $string = 
  2. "Lorem ipsum dolor sit amet, consectetur 
  3. adipiscing elit. Nunc ut elit id mi ultricies 
  4. adipiscing. Nulla facilisi. Praesent pulvinar, 
  5. sapien vel feugiat vestibulum, nulla dui pretium orci, 
  6. non ultricies elit lacus quis ante. Lorem ipsum dolor 
  7. sit amet, consectetur adipiscing elit. Aliquam 
  8. pretium ullamcorper urna quis iaculis. Etiam ac massa 
  9. sed turpis tempor luctus. Curabitur sed nibh eu elit 
  10. mollis congue. Praesent ipsum diam, consectetur vitae 
  11. ornare a, aliquam a nunc. In id magna pellentesque 
  12. tellus posuere adipiscing. Sed non mi metus, at lacinia 
  13. augue. Sed magna nisi, ornare in mollis in, mollis 
  14. sed nunc. Etiam at justo in leo congue mollis. 
  15. Nullam in neque eget metus hendrerit scelerisque 
  16. eu non enim. Ut malesuada lacus eu nulla bibendum 
  17. id euismod urna sodales. "; 
  18.   
  19. $compressed = gzcompress($string); 
  20.   
  21. echo "Original size: "strlen($string)."\n"
  22. /* 輸出原始大小 
  23. Original size: 800 
  24. */ 
  25.   
  26. echo "Compressed size: "strlen($compressed)."\n"
  27. /* 輸出壓縮后的大小 
  28. Compressed size: 418 
  29. */ 
  30.   
  31. // 解壓縮 
  32. $original = gzuncompress($compressed); 

幾乎有50% 壓縮比率。同時(shí),你還可以使用?gzencode() 和 gzdecode() 函數(shù)來壓縮,只不用其用了不同的壓縮算法。

9. 注冊(cè)停止函數(shù)

有一個(gè)函數(shù)叫做?register_shutdown_function(),可以讓你在整個(gè)腳本停時(shí)前運(yùn)行代碼。讓我們看下面的一個(gè)示例:

  1. // capture the start time 
  2. $start_time = microtime(true); 
  3.   
  4. // do some stuff 
  5. // ... 
  6.   
  7. // display how long the script took 
  8. echo "execution took: "
  9.         (microtime(true) - $start_time). 
  10.         " seconds."

上面這個(gè)示例只不過是用來計(jì)算某個(gè)函數(shù)運(yùn)行的時(shí)間。然后,如果你在函數(shù)中間調(diào)用?exit() 函數(shù),那么你的最后的代碼將不會(huì)被運(yùn)行到。并且,如果該腳本在瀏覽器終止(用戶按停止按鈕),其也無法被運(yùn)行。

而當(dāng)我們使用了register_shutdown_function()后,你的程序就算是在腳本被停止后也會(huì)被運(yùn)行:

  1. $start_time = microtime(true); 
  2.   
  3. register_shutdown_function('my_shutdown'); 
  4.   
  5. // do some stuff 
  6. // ... 
  7.   
  8. function my_shutdown() { 
  9.     global $start_time
  10.   
  11.     echo "execution took: "
  12.             (microtime(true) - $start_time). 
  13.             " seconds."

原文鏈接:http://coolshell.cn/articles/2394.html

責(zé)任編輯:陳四芳 來源: 酷殼網(wǎng)
相關(guān)推薦

2023-03-06 10:42:34

CSS前端

2023-09-07 16:28:46

JavaScrip

2013-08-23 09:28:37

GitGit 命令

2013-08-15 09:52:45

開發(fā)框架開發(fā)工具開發(fā)腳本

2015-10-27 11:02:06

Web開發(fā)CSS 庫

2011-05-16 08:37:56

JavaScript庫

2020-03-06 08:35:45

GitHub設(shè)計(jì)瀏覽器

2014-06-13 11:26:53

CSS庫Web開發(fā)

2023-06-28 00:02:40

2023-07-18 07:56:31

工具reduce業(yè)務(wù)

2013-10-29 09:24:47

Linux命令Shell腳本

2023-08-02 16:14:04

2022-06-16 08:35:10

CSS屬性前端

2021-06-29 10:50:30

Python函數(shù)文件

2011-05-10 08:47:55

開發(fā)者HTML 5W3C

2012-08-02 13:03:24

Mac OS X操作系統(tǒng)

2022-02-23 15:30:28

SpringBoot后端流程

2021-06-30 23:40:02

Wi-Fi無線路由器

2022-08-23 09:01:02

HTMLWeb

2017-10-25 16:22:58

OpenStack操作Glance
點(diǎn)贊
收藏

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