从本章中,我们了解
.创建可以调用的函数以便重用代码
.把参数传递给函数并且从函数返回值和脚本的不同部分中的变量和数据进行交互
.把代码和函数组存入到其他文件中,并且我们的脚本内包含这些文件.
3.1基本代码重用:函数
3.1.1 定义和调用函数
关键字function通知php这是一个函数,后面跟着的是函数的名称,它可以是字母、数字、字符或下划线
函数名称之后是参数列表,然后是函数体。在其它语言中名称相同、但是参数列表不同的函数,php不支持这一特性。
复制代码 代码如下:
function booo_spooky()
{
echo "I am booo_spooky. This name is okay!
\n";
}
function ____333434343434334343()
{
echo 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
\n";
}
//
// Extended characters are ok.
//
function grüß_dich()
{
echo "Extended Characters are ok, but be careful!
\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 Even Japanese characters are ok in function names, but be
extra careful with these (see Chapter 5).
EOT;
}
?>
复制代码 代码如下:
function my_new_function($param1, $param2, $param3, $param4)
{
echo You passed in:
\$param1: $param1
\$param2: $param2
\$param3: $param3
\$param4: $param4
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
\n";
function change_parameter_value($param1)
{
$param1 = 20;
}
echo "\$x is: $x
\n";
?>
复制代码 代码如下:
function increment_variable(&$increment_me)
{
if (is_int($increment_me) is_float($increment_me))
{
$increment_me += 1;
}
}
$x = 20.5;
echo "\$x is: $x
\n"; // prints 20.5
increment_variable(&$x);
echo "\$x is now: $x
\n"; // prints 21.5
?>
复制代码 代码如下:
function perform_sort($arrayData, $param2 = "qsort")
{
switch ($param)
{
case "qsort":
qsort($arrayData);
break;
case "insertion":
insertion_sort($arrayData);
break;
default:
bubble_sort($arrayData);
break;
}
}
?>
复制代码 代码如下:
function print_parameter_values()
{
$all_parameters = func_get_args();
foreach ($all_parameters as $index => $value)
{
echo "Parameter $index has the value: $value
\n";
}
echo "-----
\n";
}
print_parameter_values(1, 2, 3, "fish");
print_parameter_values();
?>
复制代码 代码如下:
function does_nothing()
{
}
$ret = does_nothing();
echo '$ret: ' . (is_null($ret) ? '(null)' : $ret) . "
";
?>
复制代码 代码如下:
function is_even_number($number)
{
if (($number % 2) == 0)
return TRUE;
else
return FALSE;
}
?>
复制代码 代码如下:
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"];
}
?>
复制代码 代码如下:
$name = "Fatima";
echo "\$name: $name
\n";
function set_name($new_name)
{
echo "\$name: $name
\n";
$name = $new_name;
}
set_name("Giorgio");
echo "\$name: $name
\n";
?>
复制代码 代码如下:
function increment_me()
{
// the value is set to 10 only once.
static $incr=10;
$incr++;
echo"$incr
\n";
}
increment_me();
increment_me();
increment_me();
?>
复制代码 代码如下:
$name = "Fatima";
echo "\$name: $name
\n";
function set_name($new_name)
{
echo "\$name: $name
\n";
$name = $new_name;
}
set_name("Giorgio");
echo "\$name: $name
\n";
?>
复制代码 代码如下:
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");
?>
复制代码 代码如下:
// circle is (x, y) + radius
function compute_circle_area($x, $y, $radius)
{
return ($radius * pi() * pi());
}
function circle_move_location(&$y, &$x, $deltax, $deltay)
{
$x += $deltax;
$y += $deltay;
}
function compute_circumference_of_circle($radius)
{
return array("Circumference" => 2 * $radius * pi());
}
?>
复制代码 代码如下:
//
// 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;
}
?>
复制代码 代码如下:
include('i_dont_exit.inc');
require('i_dont_exit.inc');\
?>
复制代码 代码如下: