9个你需要知道的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.";

相关文章

相关视频

网友评论

文明上网理性发言,请遵守 新闻评论服务协议我要评论

立即提交

专题推荐独孤九贱-php全栈开发教程

全栈 100W+

主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门

玉女心经-web前端开发教程

入门 50W+

主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门

天龙八部-实战开发教程

实战 80W+

主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习

php要懂函数吗,九个你需要知道的PHP函数和功能相关推荐

  1. php函数几种写法,PHP让人不知道的匿名函数的几种写法(附代码)

    PHP让人不知道的匿名函数的几种写法 通用的写法: 1.传统写法 function timer () { echo "hello world"; } SwooleTimer::ti ...

  2. 分享 14 个你必须知道的 JS 函数

    英文 | https://javascript.plainenglish.io/you-must-understand-these-14-javasript-functions-1f4fa1c620e ...

  3. scanf 接收 空格 输入_你需要知道的scanf函数用法

    scanf函数   上一节中我们讨论了,如何将整数,浮点数,字符串打印到屏幕上去.既然有输出,怎么能没有输入呢?这一节中,我们来介绍与printf相反的scanf函数.   scanf的功能是将键盘输 ...

  4. import是引进外部函数吗_你必须要知道的Python中的main函数

    在很多开发语言中,都有一个叫做mian的主函数,这个函数一般都是程序的入口,当程序启动时,首先执行这个函数. 比如C#语言中的main函数长这个样子. static void Main(string[ ...

  5. 32k通过地址跳转到函数_【HYPERLINK】函数技巧,你一定要知道的!!!

    Excel的HYPERLINK函数主要创建超链接.本文介绍 Microsoft Excel中HYPERLINK函数的公式语法和用法. HYPERLINK函数功能 创建一个快捷方式(跳转),用以打开存储 ...

  6. 前端需要知道的CSS函数大全

    之前一直以为css没有几个函数,今天才发现css现在竟然已经有86个函数了,意不意外,惊不惊喜!!! 我一直比较喜欢用css来解决之前js实现的效果,这样对性能时一种优化,自己也有成就感,希望这些函数 ...

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

    即使使用 PHP 多年,有些功能和特点我们也未必发现或未被充分利用,一旦被我们发现,就会发现它们非常有用.然而,并不是所有的人都已经从头至尾详读过 PHP 的手册和功能参考! 1. 函数与任意数量的参 ...

  8. 超详细!少数人才知道的 Python 函数定义详解

    Python 函数定义时支持可变数量的参数. 一.默认值参数 在 Python 中给函数的参数指定默认值是一种十分常见的使用方式,这样在调用函数时,可以使用比定义时更少的参数. 示例代码 def ch ...

  9. 九、Linux之dup和dup2函数

    九.Linux之dup和dup2函数 目录: 九.Linux之dup和dup2函数 一.重定向 二.dup()函数原型(dup--duplicate) int dup(int oldfd); 三.du ...

最新文章

  1. mysql 怎么格式化输出_怎么格式化MySQL输出代码
  2. Chrome 打印PDF技巧
  3. 工作流引擎Activiti使用总结
  4. Microsoft宣布正式发布Linux on ASE
  5. mysql的告警日志在哪_mysql日志文件在哪
  6. javaScript几种设计模式之一——单体模式
  7. 剑指offer 面试题49. 丑数
  8. 助力移动AR应用发展,阿里巴巴推出AR开放平台
  9. 重置PL/SQL Developer工具栏布局
  10. DiffMerge 工具记录
  11. 机械工程c语言第一学期考试题,科学出版社机械工程图学习题集1~10章答案.ppt
  12. 微博营销案例 – 运来大师看手相
  13. BZOJ 2037 [Sdoi2008] Sue的小球
  14. ssh-keygen命令使用
  15. 计算机桌面文件保存位置是哪里,电脑微信接收文件存放位置在哪?怎么更改文件存放位置...
  16. 周志华 机器学习初步 线性模型
  17. 多智能体强化学习:基本概念,通信方式,IPPO,MADDPG
  18. 刀片服务器接显示器,《魔兽世界》国服 退役刀片服务器开售
  19. 凯格精机通过注册:拟募资5亿 邱国良与彭小云夫妇为实控人
  20. Docker构建python环境

热门文章

  1. clientHeight、offsetHeight、scrollHeight问题
  2. leetcode 95 python
  3. python利用pandas存数据并且展示csv
  4. 贪心策略——活动选择问题
  5. 贪心算法——合并区间(Leetcode 56)
  6. kafka监听topic消费_Kafka消费者-从Kafka读取数据
  7. git进入项目目录 windows_Windows下Git 怎么整个文件夹目录上传到代码仓库(不论GitHub、GitLab、Gitee、DevCloud)...
  8. keil及iar调试解释
  9. 鸿蒙轻内核源码分析:异常钩子模块系统中断异常,如何转储异常信息
  10. 划重点!DWS开发的五大要点