`

9个你需要知道的PHP函数和功能

    博客分类:
  • PHP
阅读更多
即使使用 PHP 多年,有些功能和特点我们也未必发现或未被充分利用,一旦被我们发现,就会发现它们非常有用。然而,并不是所有的人都已经从头至尾详读过 PHP 的手册和功能参考!

1. 函数与任意数量的参数
您可能已经知道,PHP 允许我们定义可选参数的函数。但也有完全允许任意数量的函数参数方法。

首先,下面这个例子只是可选参数:

view sourceprint?01 // function with 2 optional arguments 

02 function foo($arg1 = '', $arg2 = '') { 

03     echo "arg1: $arg1\n"; 

04     echo "arg2: $arg2\n"; 

05 } 

06 foo('hello','world'); 

07 /* prints: 

08 arg1: hello 

09 arg2: world 

10 */

11 foo(); 

12 /* prints: 

13 arg1: 

14 arg2: 

15 */

现在,让我们看看如何可以建立一个函数接受任何数量的参数。这一次,我们要利用 func_get_args() 函数:

view sourceprint?01 // yes, the argument list can be empty 

02 function foo() { 

03     // returns an array of all passed arguments 

04     $args = func_get_args(); 

05     foreach ($args as $k => $v) { 

06         echo "arg".($k+1).": $v\n"; 

07     } 

08 } 

09 foo(); 

10 /* prints nothing */

11 foo('hello'); 

12 /* prints 

13 arg1: hello 

14 */

15 foo('hello', 'world', 'again'); 

16 /* prints 

17 arg1: hello 

18 arg2: world 

19 arg3: again 

20 */

2. 使用 Glob() 函数来查找文件
许多 PHP 内置函数有非常长的命名。然而,它可能会很难说明是什么作用的函数,如果不使用 Glob() 来做,除非你已经非常熟悉这个函数。

它更像是 scandir() 函数加强型版本。它可以让您通过使用模式搜索文件。

view sourceprint?01 // get all php files 

02 $files = glob('*.php'); 

03 print_r($files); 

04 /* output looks like: 

05 Array 

06 ( 

07     [0] => phptest.php 

08     [1] => pi.php 

09     [2] => post_output.php 

10     [3] => test.php 

11 ) 

12 */

这样你可以获得多个文件类型:

view sourceprint?01 // get all php files AND txt files 

02 $files = glob('*.{php,txt}', GLOB_BRACE); 

03 print_r($files); 

04 /* output looks like: 

05 Array 

06 ( 

07     [0] => phptest.php 

08     [1] => pi.php 

09     [2] => post_output.php 

10     [3] => test.php 

11     [4] => log.txt 

12     [5] => test.txt 

13 ) 

14 */

请注意,这些文件其实是可以返回一个路径的,根据你的查询。

view sourceprint?1 $files = glob('../images/a*.jpg'); 

2 print_r($files); 

3 /* output looks like: 

4 Array 

5 ( 

6     [0] => ../images/apple.jpg 

7     [1] => ../images/art.jpg 

8 ) 

9 */

如果你想获得每个文件的完整路径,你可以调用 realpath() 函数来返回。

view sourceprint?01 $files = glob('../images/a*.jpg'); 

02 // applies the function to each array element 

03 $files = array_map('realpath',$files); 

04 print_r($files); 

05 /* output looks like: 

06 Array 

07 ( 

08     [0] => C:\wamp\www\images\apple.jpg 

09     [1] => C:\wamp\www\images\art.jpg 

10 ) 

11 */

3. 内存使用信息
通过观察你的脚本内存使用情况,你就可以将你的代码进行针对性优化。

PHP 有一个垃圾收集器和一个相当复杂的内存管理器。当你的脚本开始就开始正式使用内存。也会根据脚本的执行情况,内存使用量会上升也会下降。为了得到当前内存使用情况,我们就可以使用 memory_get_usage() 函数。并可以在任何时候得到内存使用的最高点,下面就是使用 memory_get_usage() 函数的例子。

view sourceprint?01 echo "Initial: ".memory_get_usage()." bytes \n"; 

02 /* prints 

03 Initial: 361400 bytes 

04 */

05 // let's use up some memory 

06 for ($i = 0; $i <  100000; $i++) { 

07     $array []= md5($i); 

08 } 

09 // let's remove half of the array 

10 for ($i = 0; $i <  100000; $i++) { 

11     unset($array[$i]); 

12 } 

13 echo "Final: ".memory_get_usage()." bytes \n"; 

14 /* prints 

15 Final: 885912 bytes 

16 */

17 echo "Peak: ".memory_get_peak_usage()." bytes \n"; 

18 /* prints 

19 Peak: 13687072 bytes 

20 */

4. CPU 的使用信息
为此,我们就要利用 getrusage() 函数。请记住,这个函数不能应用于 windows 平台。

view sourceprint?01 print_r(getrusage()); 

02 /* prints 

03 Array 

04 ( 

05     [ru_oublock] => 0 

06     [ru_inblock] => 0 

07     [ru_msgsnd] => 2 

08     [ru_msgrcv] => 3 

09     [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 */

这看起来蛮神秘的,有些艰涩难懂,除非你已经有过系统管理员的经验,以下是每个值的介绍(或许你并不需要记住这些):

ru_oublock:块输出操作
ru_inblock:块输入操作
ru_msgsnd:邮件发送
ru_msgrcv:收到的邮件
ru_maxrss:最大驻留集大小
ru_ixrss:积分共享内存的大小
ru_idrss:积分大小非共享数据
ru_minflt:页回收
ru_majflt:页面错误
ru_nsignals:信号接收
ru_nvcsw:自动上下文切换
ru_nivcsw:非自动的上下文切换
ru_nswap:过期
ru_utime.tv_usec:用户使用时间(微秒)
ru_utime.tv_sec:用户使用时间(秒)
ru_stime.tv_usec:系统使用时间(微秒)
ru_stime.tv_sec:系统使用时间(秒)
要看 CPU 的功率有多少被脚本消耗,我们需要观察 user time 和 system time 的值。秒和毫秒是默认独立提供的。你可以将 100 万毫秒的值,并将其换算成秒的值,将它当做一个十进制数的总秒数。

让我们看一个例子:

view sourceprint?01 // sleep for 3 seconds (non-busy) 

02 sleep(3); 

03 $data = getrusage(); 

04 echo "User time: ". 

05     ($data['ru_utime.tv_sec'] + 

06     $data['ru_utime.tv_usec'] / 1000000); 

07 echo "System time: ". 

08     ($data['ru_stime.tv_sec'] + 

09     $data['ru_stime.tv_usec'] / 1000000); 

10 /* prints 

11 User time: 0.011552 

12 System time: 0 

13 */

虽然脚本大约花了 3 秒钟的时间来运行, CPU 的使用率还是非常非常低的。因为 sleep 工作,脚本实际上并没有消耗 CPU 资源。当然还有其他的任务可能真正需要等待时间,但千万不能用磁盘的读取写入操作来等待 CPU 时间。所以你可以发现, CPU 使用率和运行时的实际长度并不是总是一样的。

下面是另外一个例子。

view sourceprint?01 // loop 10 million times (busy) 

02 for($i=0;$i< 10000000;$i++) { 

03 } 

04 $data = getrusage(); 

05 echo "User time: ". 

06     ($data['ru_utime.tv_sec'] + 

07     $data['ru_utime.tv_usec'] / 1000000); 

08 echo "System time: ". 

09     ($data['ru_stime.tv_sec'] + 

10     $data['ru_stime.tv_usec'] / 1000000); 

11 /* prints 

12 User time: 1.424592 

13 System time: 0.004204 

14 */

这花了大约 1.4 秒的 CPU 时间。几乎所有这些都是由用户操作所用的时间,系统并没有被调用。

系统时间是划分时间的 CPU 上执行的程序的代表的内核系统调用时间。(谁有更简明扼要的描述?help!)下面是一个例子:

view sourceprint?01 $start = microtime(true); 

02 // keep calling microtime for about 3 seconds 

03 while(microtime(true) - $start <  3) { 

04 } 

05 $data = getrusage(); 

06 echo "User time: ". 

07     ($data['ru_utime.tv_sec'] + 

08     $data['ru_utime.tv_usec'] / 1000000); 

09 echo "System time: ". 

10     ($data['ru_stime.tv_sec'] + 

11     $data['ru_stime.tv_usec'] / 1000000); 

12 /* prints 

13 User time: 1.088171 

14 System time: 1.675315 

15 */

5. 魔术常量
PHP 提供了获取当前行号的方法 (__LINE__),获取文件路径方法(__FILE__),目录(__DIR__),函数名(__FUNCTTION__),类名(__CLASS__),方法名(__METHOD__),和命名空间(__NAMESPACE__)。以上就是常用的魔术常量。恐怕我们最常用的就只有 (__FILE__) 了。

Rikku 不打算全部进行说明,但会说几个用例。

当然包括了其他的脚本,这是个不错的主意。((__DIR__)需要 PHP 5.3 以上版本):

view sourceprint?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 // this is always relative to this file's path 

5 // no matter where it was included from 

6 require_once(dirname(__FILE__) . '/config/database.php');

使用 __LINE__ 让调试更加容易,你可以跟踪行号:

view sourceprint?01 // some code 

02 // ... 

03 my_debug("some debug message", __LINE__); 

04 /* prints 

05 Line 4: some debug message 

06 */

07 // some more code 

08 // ... 

09 my_debug("another debug message", __LINE__); 

10 /* prints 

11 Line 11: another debug message 

12 */

13 function my_debug($msg, $line) { 

14     echo "Line $line: $msg\n"; 

15 }

6. 生成唯一的ID
有些情况下,您需要生成一股唯一的字符串。我看到很多人会用这个 md5() 函数,即使他并不完全用于此目的的存在:

view sourceprint?1 // generate unique string 

2 echo md5(time() . mt_rand(1,1000000));

其实有个专门的 PHP 函数,名为 uniqid() 就是为了这个目的而存在的:

view sourceprint?01 // generate unique string 

02 echo uniqid(); 

03 /* prints 

04 4bd67c947233e 

05 */

06 // generate another unique string 

07 echo uniqid(); 

08 /* prints 

09 4bd67c9472340 

10 */

您可能会注意到,即使是唯一的字符串,他们的前几个字符很相似。这是因为生成的字符串是关联到服务器时间的。设实际上有一个非常好的副作用,因为每个新生成的 ID 将在生成后按字母顺序排列,这样也省去了我们排序的逻辑动作。

为了减少重复的几率,你可以传递一个前缀,或在第二个参数来增加。

view sourceprint?01 // with prefix 

02 echo uniqid('foo_'); 

03 /* prints 

04 foo_4bd67d6cd8b8f 

05 */

06 // with more entropy 

07 echo uniqid('',true); 

08 /* prints 

09 4bd67d6cd8b926.12135106 

10 */

11 // both 

12 echo uniqid('bar_',true); 

13 /* prints 

14 bar_4bd67da367b650.43684647 

15 */

此功能将会生成比 md5() 生成的字符串更短,这也将节省您的空间。

7. 序列化
你有没有需要存储在数据库中复杂的变量或者大文本文件?那你有没有拿出一个解决方法,花式转换成格式化的字符串数组或对象的?别担心,PHP 已经为我们准备好了这个功能。

有两种序列化的方法,下面是一个例子,它使用 serialize() 进行序列化和 unserialize() 进行解除序列化:

view sourceprint?01 // a complex array 

02 $myvar = array( 

03     'hello', 

04     42, 

05     array(1,'two'), 

06     'apple'

07 ); 

08 // convert to a string 

09 $string = serialize($myvar); 

10 echo $string; 

11 /* prints 

12 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";} 

13 */

14 // you can reproduce the original variable 

15 $newvar = unserialize($string); 

16 print_r($newvar); 

17 /* prints 

18 Array 

19 ( 

20     [0] => hello 

21     [1] => 42 

22     [2] => Array 

23         ( 

24             [0] => 1 

25             [1] => two 

26         ) 

27     [3] => apple 

28 ) 

29 */

这是原生态的 PHP 序列化方法。然而,由于 JSON 近年来已经大受欢迎,PHP 5.2 中也决定添加对它们的支持。现在你可以使用 json_encode() 和 json_decode() 函数来完成这项工作:

view sourceprint?01 // a complex array 

02 $myvar = array( 

03     'hello', 

04     42, 

05     array(1,'two'), 

06     'apple'

07 ); 

08 // convert to a string 

09 $string = json_encode($myvar); 

10 echo $string; 

11 /* prints 

12 ["hello",42,[1,"two"],"apple"] 

13 */

14 // you can reproduce the original variable 

15 $newvar = json_decode($string); 

16 print_r($newvar); 

17 /* prints 

18 Array 

19 ( 

20     [0] => hello 

21     [1] => 42 

22     [2] => Array 

23         ( 

24             [0] => 1 

25             [1] => two 

26         ) 

27     [3] => apple 

28 ) 

29 */

这么做看起来会更加紧凑。当然它对其他语言如 javascript 兼容性也是最好的。然而,您需要注意的是:对于某些复杂的对象,某些信息会无故丢失!

8. 压缩字符串
在谈到压缩时,我们通常会想到一些文件,如 zip 文件。它可以在 PHP 中压缩长字符串,并且不涉及任何存档文件。

在下面的例子中,我们要利用 gzcompress() 和 gzuncompress() 函数:

view sourceprint?01 $string = 

02 "Lorem ipsum dolor sit amet, consectetur 

03 adipiscing elit. Nunc ut elit id mi ultricies 

04 adipiscing. Nulla facilisi. Praesent pulvinar, 

05 sapien vel feugiat vestibulum, nulla dui pretium orci, 

06 non ultricies elit lacus quis ante. Lorem ipsum dolor 

07 sit amet, consectetur adipiscing elit. Aliquam 

08 pretium ullamcorper urna quis iaculis. Etiam ac massa 

09 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 $compressed = gzcompress($string); 

19 echo "Original size: ". strlen($string)."\n"; 

20 /* prints 

21 Original size: 800 

22 */

23 echo "Compressed size: ". strlen($compressed)."\n"; 

24 /* prints 

25 Compressed size: 418 

26 */

27 // getting it back 

28 $original = gzuncompress($compressed);

我们能够压缩近 50% 。另外 gzencode() 和 gzdecode() 可以达成类似的结果,但通过的是不同的压缩算法。

9. register_shutdown_function
有一个函数叫 register_shutdown_function(),可以让你在拥有执行一些代码权限之前,完成脚本的运行。

试想一下,你想捕捉到你脚本执行至结束时一些基准的统计数据,如一共用了多少时间来执行:

view sourceprint?1 // capture the start time 

2 $start_time = microtime(true); 

3 // do some stuff 

4 // ... 

5 // display how long the script took 

6 echo "execution took: ". 

7         (microtime(true) - $start_time). 

8         " seconds.";

起初觉得这些似乎是微不足道的。你只要添加代码放在底部,它运行脚本之前完成。不过,在脚本程序其中你调用了 exit() 函数,那么该段代码将不被执行。此外,如果有一个致命的错误,或者该脚本由用户终止(就是按浏览器上面的停止按钮),再次刷新页面也无法被运行。

当您使用 register_shutdown_function(),你的代码将没有理由被迫停止:

view sourceprint?01 $start_time = microtime(true); 

02 register_shutdown_function('my_shutdown'); 

03 // do some stuff 

04 // ... 

05 function my_shutdown() { 

06     global $start_time; 

07     echo "execution took: ". 

08             (microtime(true) - $start_time). 

09             " seconds."; 

分享到:
评论

相关推荐

    9个必须知道的实用PHP函数和功能

    下面将详细讲解9个必须知道的PHP函数和功能。 1. **任意参数数目的函数**: PHP允许我们创建可接受任意数量参数的函数,这得益于`func_get_args()`函数。例如,当定义一个函数时,可以不指定参数列表,而在函数...

    PHP函数库,PHP函数大全,PHP函数实例,PHP函数手册,PHP5函数库实例

    PHP函数库,PHP函数大全,PHP函数实例,PHP函数手册,PHP5函数库实例 PHP函数库,PHP函数大全,PHP函数实例,PHP函数手册,PHP5函数库实例 curl获取远程文件内容 GD显示中文 PHP GIF动画生成类 PHP HTML转UBB函数 PHP ...

    PHP上百个常用功能函数.zip

    "PHP上百个常用功能函数.zip"这个压缩包显然包含了众多实用的PHP函数和方法,旨在帮助开发者在日常工作和学习中提升效率。下面将详细讨论一些PHP中常见的功能函数,这些函数通常会在各种场景下被广泛应用。 1. **...

    (第七讲) PHP函数和自定义函数

    【PHP函数和自定义函数详解】 在编程领域,函数是代码组织的重要手段,它将一组相关的操作封装起来,便于重复使用和模块化编程。在PHP中,函数同样扮演着至关重要的角色,允许开发者以高效的方式处理任务。接下来,...

    PHP函数清单.pdf

    在实际开发过程中,对于PHP函数的理解和运用至关重要,因为PHP拥有大量的内置函数,涵盖各种功能,如数学运算、字符串处理、数组操作、文件系统交互等。 在PHP手册中,每个函数都有详细的描述,但没有提供完整的...

    7个必须知道的实用PHP函数和功能.doc

    ### 7个必须知道的实用PHP函数和功能 在PHP编程中,掌握一些核心和实用的函数能够极大地提高开发效率和代码质量。本文将详细介绍七个非常实用的PHP函数及其应用场景,帮助开发者更好地理解和运用这些工具。 #### 1...

    PHP函数实验报告

    通过以上的实验和学习,你可以更深入地理解PHP函数的使用,包括其创建、参数传递、返回值、作用域以及如何与其他语言特性结合。实践是最好的老师,尝试编写自己的函数,并在实际项目中应用它们,将有助于你成为更...

    php常用函数及其功能整理

    php常用函数及其功能整理

    php函数 参考大全

    总的来说,《PHP函数参考大全》是一个全面且实用的资源,它将帮助你构建起坚实的PHP知识体系,无论是在日常开发还是在解决复杂问题时,都能提供有力的支持。持续学习和实践,你将成为一个真正的PHP大师。

    PHP 函数索引 函数库 语法

    本文将深入探讨PHP函数索引、函数库以及语法相关的知识,帮助开发者更好地理解和利用PHP的强大功能。 首先,PHP函数是预定义的代码块,可以接受输入(参数),执行特定任务,并返回结果。函数是PHP编程的基础,它们...

    php函数 函数例子源码格式

    这些函数库手册和参考手册通常会详尽地解释每个函数的语法、参数、返回值以及示例,帮助开发者快速查找和理解所需功能。通过阅读和实践其中的源码例子,可以加深对PHP函数的理解,提高编程效率。在日常工作中,它们...

    PHP函数速查效率手册 source code

    为了方便读者学习和程序开发,本书全面介绍了各类PHP函数。本书共分21章,PHP函数所涉及的范围包括MySQL服务器、Apache服务器、ODBC、String字符串、Array数组、Variable函数、时间日期、Hash、正则表达式、Session...

    一个简单的 php 分页 函数

    - **分页函数文件**:这是一个PHP函数,接收总记录数、每页记录数和当前页数作为参数,返回用于构建分页链接的HTML代码和调整后的SQL查询语句。 - 函数可能首先计算总页数,公式为`ceil(total_rows / per_page)`,...

    PHP函数参考手册

    * AJAX 应用:PHP 和 AJAX 结合可以实现多种动态网页更新功能,例如实时搜索、自动完成等。 PHP 参考手册知识点: * PHP Array 函数:PHP Array 函数用于操作数组,例如创建、合并、过滤、排序等操作。 * PHP ...

    PHP 文件操作函数

    在PHP编程语言中,文件操作是至关重要的一个环节,它涉及到读取、写入、创建、删除、移动和复制文件等任务。这篇博客主要探讨了PHP中的文件操作函数,通过学习这些函数,开发者可以高效地处理服务器上的文件系统。...

    非常实用的PHP函数大全

    "非常实用的PHP函数大全"这个资源包含了多个常用且功能丰富的PHP函数,旨在帮助开发者快速查找和理解这些函数的用途。下面将详细介绍一些重要的PHP函数。 1. **echo与print**:这两个函数都是用于输出数据,但echo...

    php函数手册.rar

    PHP函数是PHP编程的核心组成部分,它们提供了丰富的功能,包括数据处理、文件操作、网络通信、数据库交互等。手册中的每个函数通常会包含以下几个方面: 1. **函数名**:这是调用函数时使用的标识符,例如`strlen()...

    PHP函数.doc

    PHP函数是PHP编程中不可或缺的一部分,它们提供了丰富的功能,帮助开发者执行各种任务。以下是一些在描述中提及的PHP函数的详细说明: 1. `usleep()`:此函数用于延迟代码执行,参数是以微秒为单位的时间。例如,`...

Global site tag (gtag.js) - Google Analytics