函數名稱之後是參數列表,然後是函數體。在其它語言中名稱相同、但是參數列表不同的函數,php不支援這一特性。
複製代碼 代碼如下:
<?php
function booo_spooky()
{
echo "I am booo_spooky. This name is okay!<br/>\n";
}
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/>\n";
}
//
// Extended characters are ok.
//
function grüß_dich()
{
echo "Extended Characters are ok, but be careful!<br/>\n";
}
//
// 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/>\n";
function change_parameter_value($param1)
{
$param1 = 20;
}
echo "\$x is: $x<br/>\n";
?>
輸出: $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/>\n"; // prints 20.5
increment_variable(&$x);
echo "\$x is now: $x <br/>\n"; // 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/>\n";
}
echo "-----<br/>\n";
}
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/>\n";
}
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;
}
?>