php允许传递任意个参数:
function getArg($a="",$b=""){ echo "one:".$a."<br/>"; echo "two".$b; }
getArg(100,200);
调用函数结果:
one:100
two:200
function foo() {
// 取得所有的传入参数的数组
$args = func_get_args();
foreach ($args as $k => $v) {
echo “arg”.($k+1).”: $v\n”;
}
}
foo();
/* 什么也不会输出 */
foo(‘hello’);
/* 输出
arg1: hello
*/
foo(‘hello’, ‘world’, ‘again’);
/* 输出
arg1: hello
arg2: world
arg3: again
*/
// 取得所有的后缀为PHP的文件 $files = glob(‘*.php’); print_r($files); 输出: Array ( [0] => phptest.php [1] => pi.php [2] => post_output.php [3] => test.php )
你还可以查找多种后缀名:
// 取PHP文件和TXT文件 $files = glob(‘*.{php,txt}’, GLOB_BRACE); print_r($files); /* 输出: 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’); // 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 ) */
其实PHP中自带一个函数来生成唯一的id,这个函数就是uniqid()。下面是用法:
echo uniqid(); /* 输出 4bd67c947233e */
字符串压缩
当我们说到压缩,我们可能会想到文件压缩,其实,字符串也是可以压缩的。PHP提供了 gzcompress() 和gzuncompress() 函数