成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

九個PHP很有用的功能

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

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

1. 函數的任意數目的參數

你可能知道PHP允許你定義一個默認參數的函數。但你可能并不知道PHP還允許你定義一個完全任意的參數的函數

下面是一個示例向你展示了默認參數的函數:

  1. // 兩個默認參數的函數 
  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: 

現在我們來看一看一個不定參數的函數,其使用到了?func_get_args()方法:

  1. // 是的,形參列表為空 
  2. function foo() { 
  3.   
  4.     // 取得所有的傳入參數的數組 
  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. /* 什么也不會輸出 */ 
  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的函數都有一個比較長的自解釋的函數名,但是,當你看到?glob() 的時候,你可能并不知道這個函數是用來干什么的,除非你對它已經很熟悉了。

你可以認為這個函數就好?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. */ 

如果你想得到絕對路徑,你可以調用?realpath() 函數:

  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. 內存使用信息

觀察你程序的內存使用能夠讓你更好的優化你的代碼。

PHP 是有垃圾回收機制的,而且有一套很復雜的內存管理機制。你可以知道你的腳本所使用的內存情況。要知道當前內存使用情況,你可以使用? memory_get_usage() 函數,如果你想知道使用內存的峰值,你可以調用memory_get_peak_usage() 函數。

  1. echo "Initial: ".memory_get_usage()." bytes \n"
  2. /* 輸出 
  3. Initial: 361400 bytes 
  4. */ 
  5.   
  6. // 使用內存 
  7. for ($i = 0; $i < 100000; $i++) { 
  8.     $array []= md5($i); 
  9.   
  10. // 刪除一半的內存 
  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() 函數可以讓你知道CPU的使用情況。注意,這個功能在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. */ 

這個結構看上出很晦澀,除非你對CPU很了解。下面一些解釋:

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

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

  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是不占用系統時間的,我們可以來看下面的一個例子:

  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時間,幾乎所有的都是用戶的時間,因為沒有系統調用。

系統時間是CPU花費在系統調用上的上執行內核指令的時間。下面是一個例子:

  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. */ 

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

5. 系統常量

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

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

  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的信息,這樣有助于你調試程序:

  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() 來生成一個唯一的ID,如下所示:

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

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

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

可能你會注意到生成出來的ID前幾位是一樣的,這是因為生成器依賴于系統的時間,這其實是一個非常不錯的功能,因為你是很容易為你的這些ID排序的。這點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會比MD5生成的要短,這會讓你節省很多空間。

7. 序列化

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

  1. // 一個復雜的數組 
  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的原生函數,然而在今天JSON越來越流行,所以在PHP5.2以后,PHP開始支持JSON,你可以使用 json_encode() 和 json_decode() 函數

  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和其它語言。但是對于一些非常復雜的數據結構,可能會造成數據丟失。

8. 字符串壓縮

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

  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% 壓縮比率。同時,你還可以使用?gzencode() 和 gzdecode() 函數來壓縮,只不用其用了不同的壓縮算法。

9. 注冊停止函數

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

  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."

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

而當我們使用了register_shutdown_function()后,你的程序就算是在腳本被停止后也會被運行:

  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

責任編輯:陳四芳 來源: 酷殼網
相關推薦

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

開發框架開發工具開發腳本

2011-05-16 08:37:56

JavaScript庫

2015-10-27 11:02:06

Web開發CSS 庫

2014-06-13 11:26:53

CSS庫Web開發

2020-03-06 08:35:45

GitHub設計瀏覽器

2023-06-28 00:02:40

2023-07-18 07:56:31

工具reduce業務

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函數文件

2011-05-10 08:47:55

開發者HTML 5W3C

2012-08-02 13:03:24

Mac OS X操作系統

2022-02-23 15:30:28

SpringBoot后端流程

2022-08-23 09:01:02

HTMLWeb

2021-06-30 23:40:02

Wi-Fi無線路由器

2017-10-25 16:22:58

OpenStack操作Glance
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩中文一区 | 黄色在线观看网址 | 国产 欧美 日韩 一区 | 久久久精品 | 日韩高清电影 | 国产精品久久久久久久岛一牛影视 | 在线婷婷 | 久久新视频| 国产精品极品美女在线观看免费 | 久久精品国产a三级三级三级 | 天天看天天爽 | 国产视频线观看永久免费 | av网站在线免费观看 | 国产精品日本一区二区在线播放 | 亚洲一区 中文字幕 | 欧美国产视频 | 日韩精品一区二区三区在线观看 | 久久久久国产一级毛片 | 精品欧美视频 | 国产麻豆乱码精品一区二区三区 | 日韩美女一区二区三区在线观看 | 日韩精品视频在线播放 | 精品久久久久久久久久 | 黄a在线观看 | 日韩视频在线观看中文字幕 | 两性午夜视频 | 午夜丰满少妇一级毛片 | 老头搡老女人毛片视频在线看 | 成年人在线视频 | 黄色av观看 | 国产伦精品一区二区三区高清 | 黄色一级免费看 | 麻豆精品国产91久久久久久 | 天天综合天天 | 亚洲精品欧美一区二区三区 | 国产高清精品一区二区三区 | 国产一级片一区二区 | 91免费电影 | 久久tv在线观看 | 国产一级大片 | 成年人精品视频 |