PHP9个有用的功能或者函数

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

源文:http://net.tutsplus.com/tutorials/php/9-useful-php-functions-and-features-you-need-to-know/

posted @ 2011-03-01 14:29  wgw8299  阅读(701)  评论(0编辑  收藏  举报