即使使用 PHP 多年,也会偶然发现一些未曾了解的函数和功能。其中有些是非常有用的,但没有得到充分利用。并不是所有人都会从头到尾一页一页地阅读手册和函数参考!  
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() 函数: 
 
这点和javascript有点相似,javascript实现函数的默认参数也是这样的。 
 
| 以下为引用的内容:// 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();/* prints nothing */
 foo('hello');/* prints
 arg1: hello
 */
 foo('hello', 'world', 'again');/* prints
 arg1: hello
 arg2: world
 arg3: again
 */
 
 | 
  
延伸阅读:php数组应用技巧php技巧:ini_get的用法strtotime的使用技巧实例详解PHP serialize与JSON解析PHP序列化 serialize 格式详解php中fread()函数使用技巧foreach遍历数组时使用引用的技巧php时间函数使用技巧PHP中JSON技巧讲解