<?php
function does_nothing()
{
}
$ret = does_nothing();
echo '$ret: ' . (is_null($ret) ? '(null)' : $ret) . "<br/>";
?>
如果希望返回非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"];
}
?>
3.1.4 函数内的变量范围
函数级别变量:
声明它们的函数内是合法,并且在函数的调用之间不记忆它们的值
复制代码 代码如下:
<?php
$name = "Fatima";
echo "\$name: $name<br/>\n";
function set_name($new_name)
{
echo "\$name: $name<br/>\n";
$name = $new_name;
}
set_name("Giorgio");
echo "\$name: $name<br/>\n";
?>
静态变量:
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();
?>
脚本内声明的变量("全局变量")
复制代码 代码如下:
<?php
$name = "Fatima";
echo "\$name: $name<br/>\n";
function set_name($new_name)
{
echo "\$name: $name<br/>\n";
$name = $new_name;
}
set_name("Giorgio");
echo "\$name: $name<br/>\n";
?>
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包含很多不能用作变量函数的语言结构,这种结构的明显例子是echo、print、var_dump、print_r、isset、unset、is_null is_type
3.2 中级代码重用:使用和包含文件
3.2.1 把代码组织到文件中
对通用功能进行分组: 如果希望把很多函数保存到单一位置上,典型情况是一个文件,即代码库(code library)
生成一致的接口
复制代码 代码如下:
<?php
// 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());
}
?>
通过使用这此函数具有一致的名称、参数顺序以及返回值 ,可以显著地减少失败的可能性和代码中的缺陷。
复制代码 代码如下:
<?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;
}
?>
3.2.2 选择文件名和位置
为了防止web用户打开.inc文件,我们使用两种机制防止这种情况发生,首先,在构成文档目录树中,我们确保web服务器不允许用户浏览或者加载
不希望他们进行这些操作,在16章保护web应用程序中介绍,然后,然后将配置浏览器允许用户浏览.php和.html文件,但是不能浏览.inc文件
防止这种问题的第二个途径不把代码入在文档树中,或存入其它目录,并且要么明确地在我们的代码中引用这个目录,通知php总是查看这个目录
3.2.3 在脚本中包含库文件
include 和require,这两个区别在于,当找不到文件时,require输出错误,而include输出警告。
复制代码 代码如下: