函数名称之后是参数列表,然后是函数体。在其它语言中名称相同、但是参数列表不同的函数,php不支持这一特性。 复制代码 代码如下: <?php function booo_spooky() { echo "I am booo_spooky. This name is okay!<br/>
"; } function ____333434343434334343() { echo <<<DONE I am ____333434343434334343. This is an awfully unreadable function name. But it is valid. DONE; } // // This next function name generates: // // Parse error: syntax error, unexpected T_LNUMBER, // expecting T_STRING in // /home/httpd/www/phpwebapps/src/chapter03/playing.php // on line 55 // // Function names cannot start with numbers // function 234letters() { echo "I am not valid<br/>
"; } // // Extended characters are ok. // function grüß_dich() { echo "Extended Characters are ok, but be careful!<br/>
"; } // // REALLY extended characters are ok too!! Your file will // probably have to be saved in a Unicode format though, // such as UTF-8 (See Chapter 5). // function 日本語のファンクション() { echo <<<EOT Even Japanese characters are ok in function names, but be extra careful with these (see Chapter 5). EOT; } ?>
3.1.2 把参数传递给函数 基本语法:为了把参数传递给函数,在调用函数时需要把参数值 括在括号中,以逗号分隔。每个被传递的参数可 以是任何合法表达式,可以是变量、常量值、运算符的结果,甚至可以是函数调用。 复制代码 代码如下: <?php function my_new_function($param1, $param2, $param3, $param4) { echo <<<DONE You passed in: <br/> $param1: $param1 <br/> $param2: $param2 <br/> $param3: $param3 <br/> $param4: $param4 <br/> DONE; } // // call my new function with some values. // $userName = "bobo"; $a = 54; $b = TRUE; my_new_function($userName, 6.22e23, pi(), $a or $b); ?>
按引用传递:默认情况下,只有变量的值被传递给函数。因此,对这个参数或者变量的任何改动都只是在函数局部有效的 复制代码 代码如下: $x = 10; echo "$x is: $x<br/>
"; function change_parameter_value($param1) { $param1 = 20; } echo "$x is: $x<br/>
"; ?>
输出: $x is :10 $x is :10 如果你的目的是函数实际地修改传递给它的变量,而不仅仅处理其值的拷贝,那么可以用引用(reference)传递的功能。这是通过使用&字符完成的
复制代码 代码如下: <?php function increment_variable(&$increment_me) { if (is_int($increment_me) || is_float($increment_me)) { $increment_me += 1; } } $x = 20.5; echo "$x is: $x <br/>
"; // prints 20.5 increment_variable(&$x); echo "$x is now: $x <br/>
"; // prints 21.5 ?>
参数的默认值 在你期望参数具有支配地位的特定值的情况下,称为默认参数值(default argumentvalue) 复制代码 代码如下: <?php function perform_sort($arrayData, $param2 = "qsort") { switch ($param) { case "qsort": qsort($arrayData); break; case "insertion": insertion_sort($arrayData); break; default: bubble_sort($arrayData); break; } } ?>
可变数量的参数: php能够把任意数量的参数传递给函数,然后使用func_num_args、func_get_arg和func_get_args取得参数值 复制代码 代码如下: <?php function print_parameter_values() { $all_parameters = func_get_args(); foreach ($all_parameters as $index => $value) { echo "Parameter $index has the value: $value<br/>
"; } echo "-----<br/>
"; } print_parameter_values(1, 2, 3, "fish"); print_parameter_values(); ?>
如果希望返回非null时,利用return把它和一个表达式关联 复制代码 代码如下: <?php function is_even_number($number) { if (($number % 2) == 0) return TRUE; else return FALSE; } ?>
当你希望从函数返回多个值 时,把结果作为数组传递回来是方便的方式 复制代码 代码如下: <?php function get_user_name($userid) { // // $all_user_data is a local variable (array) that temporarily // holds all the information about a user. // $all_user_data = get_user_data_from_db($userid); // // after this function returns, $all_user_data no // longer exists and has no value. // return $all_user_data["UserName"]; } ?>
静态变量: static作为前缀的变量在函数调用之间保持它们的值不变,如果声明变量时为其赋值了,在运行当前脚本时,php只在第一次遇到这个变量时执行赋值 复制代码 代码如下: <?php function increment_me() { // the value is set to 10 only once. static $incr=10; $incr++; echo"$incr<br/>
"; } increment_me(); increment_me(); increment_me(); ?>
l输出结果: $name: Fatima $name: $name: Fatima 如果在 内部组函数加一个globa ,那么输出结果 $name: Fatima $name: Fatima $name: Giorgio 3.1.5 函数范围和可用性 3.1.6 把函数作为变量使用 复制代码 代码如下: <?php function Log_to_File($message) { // open file and write message } function Log_to_Browser($message) { // output using echo or print functions } function Log_to_Network($message) { // connect to server and print message } // // we"re debugging now, so we"ll just write to the screen // $log_type = "Log_to_Browser"; // // now, throughout the rest of our code, we can just call // $log_type(message) and change where it goes by simply // changing the above variable assignment! // $log_type("beginning debug output"); ?>
通过使用这此函数具有一致的名称、参数顺序以及返回值 ,可以显著地减少失败的可能性和代码中的缺陷。 复制代码 代码如下: <?php // // all routines in this file assume a circle is passed in as // an array with: // "X" => x coord "Y" => y coord "Radius" => circle radius // function circles_compute_area($circle) { return $circle["Radius"] * $circle["Radius"] * pi(); } function circles_compute_circumference($circle) { return 2 * $circle["Radius"] * pi(); } // $circle is passed in BY REFERENCE and modified!!! function circles_move_circle(&$circle, $deltax, $deltay) { $circle["X"] += $deltax; $circle["Y"] += $deltay; } ?>