通过参数列表可以传递信息到函数,该列表是以逗号作为分隔符的变量和常量列表。
   
     PHP 支持按值传递参数(默认),
     通过引用传递, 和
     默认参数值.
     可变长度参数列表仅在 PHP 4 和后续版本中支持;更多信息请参照 可变长度参数列表
     和涉及到的相关函数 func_num_args(),
     func_get_arg(), 和
     func_get_args()。
     PHP 3 中通过传递一个数组参数可以达到类似的效果:
    
    
| 例子 12-4. 向函数传递数组 | 
<?phpfunction takes_array($input)
 {
 echo "$input[0] + $input[1] = ", $input[0]+$input[1];
 }
 ?>
 | 
 | 
   
     缺省情况下,函数参数通过值传递(因而即使在函数内部改变参数的值,它
     并不会改变函数外部的值)。如果你希望允许函数修改它的参数值,你必须
     通过引用传递参数。
    
     如果想要函数的一个参数总是通过引用传递,你可以在函数定义中该参数的
     前面预先加上符号(&):
    
     
| 例子 12-5. 用引用传递函数参数 | 
<?phpfunction add_some_extra(&$string)
 {
 $string .= 'and something extra.';
 }
 $str = 'This is a string, ';
 add_some_extra($str);
 echo $str;    // outputs 'This is a string, and something extra.'
 ?>
 | 
 | 
    
     函数可以定义 C++ 风格的标量参数默认值,如下:
    
     
| 例子 12-6. 函数中默认参数的用途 | 
<?phpfunction makecoffee ($type = "cappuccino")
 {
 return "Making a cup of $type.\n";
 }
 echo makecoffee ();
 echo makecoffee ("espresso");
 ?>
 | 
 | 
    
     上述片断的输出是:
    
     
| Making a cup of cappuccino.
Making a cup of espresso. | 
    
     默认值必须是常量表达式,不是(比如)变量,类成员,或者函数调用。
    
     请注意当使用默认参数时,任何默认参数必须放在任何非默认参数的右侧;否则,
     可能函数将不会按照预期的情况工作。考虑下面的代码片断:
    
     
| 例子 12-7. 函数默认参数不正确的用法 | 
<?phpfunction makeyogurt ($type = "acidophilus", $flavour)
 {
 return "Making a bowl of $type $flavour.\n";
 }
 
 echo makeyogurt ("raspberry");   // won't work as expected
 ?>
 | 
 | 
    
     上述例子的输出时:
    
     
| Warning: Missing argument 2 in call to makeyogurt() in
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Making a bowl of raspberry . | 
    
     现在,比较上面的例子和这个例子:
    
     
| 例子 12-8. 函数默认参数正确的用法 | 
<?phpfunction makeyogurt ($flavour, $type = "acidophilus")
 {
 return "Making a bowl of $type $flavour.\n";
 }
 
 echo makeyogurt ("raspberry");   // works as expected
 ?>
 | 
 | 
    
     这个例子的输出是:
    
     
| Making a bowl of acidophilus raspberry. |