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

即使使用 PHP 多年,也会偶然发现一些未曾了解的函数和功能。其中有些是非常有用的,但没有得到充分利用。并不是所有人都会从头到尾一页一页地阅读手册和函数参考!

1、任意参数数目的函数

你可能已经知道,PHP 允许定义可选参数的函数。但也有完全允许任意数目的函数参数的方法。以下是可选参数的例子:

 

以下为引用的内容:

// function with 2 optional arguments
function foo($arg1 = '', $arg2 = '') {

 echo "arg1: $arg1\n";
 echo "arg2: $arg2\n";

}

foo('hello','world');

foo();

现在让我们看看如何建立能够接受任何参数数目的函数。这一次需要使用 func_get_args() 函数:

 

以下为引用的内容:

// yes, the argument list can be empty
function foo() {

 // returns an array of all passed arguments
 $args = func_get_args();

 foreach ($args as $k => $v) {
  echo "arg".($k+1).": $v\n";
 }

}

foo();

foo('hello');

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

2、使用 Glob() 查找文件

许多 PHP 函数具有长描述性的名称。然而可能会很难说出 glob() 函数能做的事情,除非你已经通过多次使用并熟悉了它。可以把它看作是比 scandir() 函数更强大的版本,可以按照某种模式搜索文件。

 

以下为引用的内容:

// get all php files
$files = glob('*.php');

print_r($files);

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

 

以下为引用的内容:

// get all php files AND txt files
$files = glob('*.{php,txt}', GLOB_BRACE);

print_r($files);

请注意,这些文件其实是可以返回一个路径,这取决于查询条件:

 

以下为引用的内容:

$files = glob('../images/a*.jpg');

print_r($files);

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

 

以下为引用的内容:

$files = glob('../images/a*.jpg');

// applies the function to each array element
$files = array_map('realpath',$files);

print_r($files);

 

3、内存使用信息

通过侦测脚本的内存使用情况,有利于代码的优化。PHP 提供了一个垃圾收集器和一个非常复杂的内存管理器。脚本执行时所使用的内存量,有升有跌。为了得到当前的内存使用情况,我们可以使用 memory_get_usage() 函数。如果需要获得任意时间点的最高内存使用量,则可以使用 memory_limit() 函数。

 

以下为引用的内容:

echo "Initial: ".memory_get_usage()." bytes \n";

// 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";

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

4、CPU 使用信息

为此,我们要利用 getrusage() 函数。请记住这个函数不适用于 Windows 平台。

 

以下为引用的内容:

print_r(getrusage());
这可能看起来有点神秘,除非你已经有系统管理员权限。以下是每个值的具体说明(你不需要记住这些):

 

以下为引用的内容:

ru_oublock: block output operations
ru_inblock: block input operations
ru_msgsnd: messages sent
ru_msgrcv: messages received
ru_maxrss: maximum resident set size
ru_ixrss: integral shared memory size
ru_idrss: integral unshared data size
ru_minflt: page reclaims
ru_majflt: page faults
ru_nsignals: signals received
ru_nvcsw: voluntary context switches
ru_nivcsw: involuntary context switches
ru_nswap: swaps
ru_utime.tv_usec: user time used (microseconds)
ru_utime.tv_sec: user time used (seconds)
ru_stime.tv_usec: system time used (microseconds)
ru_stime.tv_sec: system time used (seconds)

要知道脚本消耗多少 CPU 功率,我们需要看看 ‘user time’ 和 ’system time’ 两个参数的值。秒和微秒部分默认是单独提供的。你可以除以 100 万微秒,并加上秒的参数值,得到一个十进制的总秒数。让我们来看一个例子:

 

以下为引用的内容:

// sleep for 3 seconds (non-busy)
sleep(3);

$data = getrusage();
echo "User time: ".
 ($data['ru_utime.tv_sec'] +
 $data['ru_utime.tv_usec'] / 1000000);
echo "System time: ".
 ($data['ru_stime.tv_sec'] +
 $data['ru_stime.tv_usec'] / 1000000);

 

尽管脚本运行用了大约 3 秒钟,CPU 使用率却非常非常低。因为在睡眠运行的过程中,该脚本实际上不消耗 CPU 资源。还有许多其他的任务,可能需要一段时间,但不占用类似等待磁盘操作等 CPU 时间。因此正如你所看到的,CPU 使用率和运行时间的实际长度并不总是相同的。下面是一个例子:

 

以下为引用的内容:

// loop 10 million times (busy)
for($i=0;$i<10000000;$i++) {

}

$data = getrusage();
echo "User time: ".
 ($data['ru_utime.tv_sec'] +
 $data['ru_utime.tv_usec'] / 1000000);
echo "System time: ".
 ($data['ru_stime.tv_sec'] +
 $data['ru_stime.tv_usec'] / 1000000);

 

这花了大约 1.4 秒的 CPU 时间,但几乎都是用户时间,因为没有系统调用。系统时间是指花费在执行程序的系统调用时的 CPU 开销。下面是一个例子:

 

以下为引用的内容:

$start = microtime(true);
// keep calling microtime for about 3 seconds
while(microtime(true) - $start < 3) {

}

$data = getrusage();
echo "User time: ".
 ($data['ru_utime.tv_sec'] +
 $data['ru_utime.tv_usec'] / 1000000);
echo "System time: ".
 ($data['ru_stime.tv_sec'] +
 $data['ru_stime.tv_usec'] / 1000000);

 

现在我们有相当多的系统时间占用。这是因为脚本多次调用 microtime() 函数,该函数需要向操作系统发出请求,以获取所需时间。你也可能会注意到运行时间加起来不到 3 秒。这是因为有可能在服务器上同时存在其他进程,并且脚本没有 100% 使用 CPU 的整个 3 秒持续时间。

你可能感兴趣的:(PHP,windows,脚本,UP)