博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP9个有用的功能或者函数
阅读量:6975 次
发布时间:2019-06-27

本文共 2874 字,大约阅读时间需要 9 分钟。

  hot3.png

1。任意参数方式调用函数  

   你可能已经知道PHP允许您定义在函数的可选的参数。但是,也有一种允许完全任意数量的函数参数。  

首先,这里是一个例子是可选的自变量:

// function with 2 optional arguments  function foo($arg1 = '', $arg2 = '') {        echo "arg1: $arg1\n";      echo "arg2: $arg2\n";    }    foo('hello','world');  /* prints: arg1: hello arg2: world */    foo();  /* prints: arg1: arg2: */

现在,让我们看看如何建造功能接受任何数目的参数。这次我们将利用func_get_args():

function foo() {      $args = func_get_args();        foreach ($args as $k => $v) {          echo "arg".($k+1).": $v\n";      }    }    foo();  /* prints nothing */    foo('hello');  /* prints arg1: hello */    foo('hello', 'world', 'again');  /* prints arg1: hello arg2: world arg3: again */

2. 使用Glob()找到的文件

许多PHP函式一直和由描述性的名字。然而可能很难知道一个函数命名为glob()并,除非你已经熟悉那个术语是从别处搬来的。  

认为它像一个更有能力版本的函数的功能。它可以让你使用搜索文件模式。

/ get all php files  $files = glob('*.php');    print_r($files);  /* output looks like: Array (     [0] => phptest.php     [1] => pi.php     [2] => post_output.php     [3] => test.php ) */

多种类型的文件,这样的:

// get all php files AND txt files  $files = glob('*.{php,txt}', GLOB_BRACE);    print_r($files);  /* output looks like: Array (     [0] => phptest.php     [1] => pi.php     [2] => post_output.php     [3] => test.php     [4] => log.txt     [5] => test.txt ) */

注意文件可以返回路径,取决于你的查询:

$files = glob('../images/a*.jpg');    print_r($files);  /* output looks like: Array (     [0] => ../images/apple.jpg     [1] => ../images/art.jpg ) */

如果你想得到realpath()每个文件,你可以调整用函数的作用是:

$files = glob('../images/a*.jpg');    // applies the function to each array element  $files = array_map('realpath',$files);    print_r($files);  /* output looks like: Array (     [0] => C:\wamp\www\images\apple.jpg     [1] => C:\wamp\www\images\art.jpg ) */

3。内存使用信息  通过观察你的内存的使用脚本,你可以优化你的代码更好。  

PHP有的垃圾回收和一堆复杂的内存管理器。

记忆的数量正利用你的script.可以上下执行过程中脚本。去得到当前的内存的使用,我们可以用memory_get_usage函数,并得到最大数量的内存占用在任何时候,我们可以用memory_get_peak_usage()功能。

echo "Initial: ".memory_get_usage()." bytes \n";  /* prints Initial: 361400 bytes */    // let's use up some memory  for ($i = 0; $i < 100000; $i++) {      $array []= md5($i);  }    // let's remove half of the array  for ($i = 0; $i < 100000; $i++) {      unset($array[$i]);  }    echo "Final: ".memory_get_usage()." bytes \n";  /* prints Final: 885912 bytes */    echo "Peak: ".memory_get_peak_usage()." bytes \n";  /* prints Peak: 13687072 bytes */

4。CPU使用信息  

我们要利用getrusage()功能。记住这是无法提供Windows平台。

print_r(getrusage());  /* prints Array (     [ru_oublock] => 0     [ru_inblock] => 0     [ru_msgsnd] => 2     [ru_msgrcv] => 3     [ru_maxrss] => 12692     [ru_ixrss] => 764     [ru_idrss] => 3864     [ru_minflt] => 94     [ru_majflt] => 0     [ru_nsignals] => 1     [ru_nvcsw] => 67     [ru_nivcsw] => 4     [ru_nswap] => 0     [ru_utime.tv_usec] => 0     [ru_utime.tv_sec] => 0     [ru_stime.tv_usec] => 6269     [ru_stime.tv_sec] => 0 )  */

源文:

转载于:https://my.oschina.net/tiwer/blog/199848

你可能感兴趣的文章
关于nodejs中的async/await
查看>>
待总结...
查看>>
了解ASP.NET5 Web应用程序结构
查看>>
virtualbox之usb设备的分配
查看>>
Week7阅读笔记
查看>>
iOS文字滚动效果 之 横向滚动
查看>>
LayoutInflater的作用
查看>>
Oracle 11g 配置方面的问题
查看>>
笔记:《零基础搞定机器学习及深度学习(覃秉丰)》
查看>>
C++ 编写动态链接库和静态链接库
查看>>
灰色头像
查看>>
JAVAWEB 一一框架整合(SSI : Spring+SpringMVC+ ibtis)
查看>>
Win10运行在哪里,Win10的运行怎么打开
查看>>
oracle如何删除表空间
查看>>
js 用touch事件实现简单tap
查看>>
工欲善其事,必先利其器 之 VS2013全攻略(安装,技巧,快捷键,插件)!
查看>>
CentOS 6.5添加网易163源
查看>>
ClientDataSet中撤消与恢复操作及执行细节
查看>>
数据库连接池
查看>>
经典C语言程序设计100例(循序渐进,由浅入深,不为应试,只为知识)
查看>>