lwcompany

功到自然成

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

从本章中,我们了解

.创建可以调用的函数以便重用代码

.把参数传递给函数并且从函数返回值和脚本的不同部分中的变量和数据进行交互

.把代码和函数组存入到其他文件中,并且我们的脚本内包含这些文件.

3.1基本代码重用:函数

      3.1.1 定义和调用函数

                                   关键字function通知php这是一个函数,后面跟着的是函数的名称,它可以是字母、数字、字符或下划线

                                  函数名称之后是参数列表,然后是函数体。在其它语言中名称相同、但是参数列表不同的函数,php不支持这一特性。                                  

1 <?php
2
3 function booo_spooky()
4 {
5 echo "I am booo_spooky. This name is okay!<br/>\n";
6 }
7
8 function ____333434343434334343()
9 {
10 echo <<<DONE
11 I am ____333434343434334343. This is an awfully
12 unreadable function name. But it is valid.
13  DONE;
14 }
15
16  //
17 // This next function name generates:
18 //
19 // Parse error: syntax error, unexpected T_LNUMBER,
20 // expecting T_STRING in
21 // /home/httpd/www/phpwebapps/src/chapter03/playing.php
22 // on line 55
23 //
24 // Function names cannot start with numbers
25 //
26   function 234letters()
27 {
28 echo "I am not valid<br/>\n";
29 }
30
31  //
32 // Extended characters are ok.
33 //
34   function grüß_dich()
35 {
36 echo "Extended Characters are ok, but be careful!<br/>\n";
37 }
38
39  //
40 // REALLY extended characters are ok too!! Your file will
41 // probably have to be saved in a Unicode format though,
42 // such as UTF-8 (See Chapter 5).
43 //
44   function 日本語のファンクション()
45 {
46 echo <<<EOT
47 Even Japanese characters are ok in function names, but be
48 extra careful with these (see Chapter 5).
49 EOT;
50 }
51
52  ?>

                                                       

      3.1.2 把参数传递给函数

                                    基本语法:为了把参数传递给函数,在调用函数时需要把参数值 括在括号中,以逗号分隔。每个被传递的参数可

                                                  以是任何合法表达式,可以是变量、常量值、运算符的结果,甚至可以是函数调用。

1 <?php
2
3 function my_new_function($param1, $param2, $param3, $param4)
4 {
5 echo <<<DONE
6
7 You passed in: <br/>
8 \$param1: $param1 <br/>
9 \$param2: $param2 <br/>
10 \$param3: $param3 <br/>
11 \$param4: $param4 <br/>
12  DONE;
13 }
14
15
16
17 //
18 // call my new function with some values.
19 //
20   $userName = "bobo";
21 $a = 54;
22 $b = TRUE;
23 my_new_function($userName, 6.22e23, pi(), $a or $b);
24
25
26 ?>

                                 按引用传递:默认情况下,只有变量的值被传递给函数。因此,对这个参数或者变量的任何改动都只是在函数局部有效的

  

1 $x = 10;
2 echo "\$x is: $x<br/>\n";
3
4 function change_parameter_value($param1)
5 {
6 $param1 = 20;
7 }
8
9 echo "\$x is: $x<br/>\n";
10
11 ?>

                        输出: $x is :10

                                $x is :10

                                  如果你的目的是函数实际地修改传递给它的变量,而不仅仅处理其值的拷贝,那么可以用引用(reference)传递的功能。这是通过使用&字符完成的

1 <?php
2
3 function increment_variable(&$increment_me)
4 {
5 if (is_int($increment_me) || is_float($increment_me))
6 {
7 $increment_me += 1;
8 }
9 }
10
11 $x = 20.5;
12 echo "\$x is: $x <br/>\n"; // prints 20.5
13 increment_variable(&$x);
14 echo "\$x is now: $x <br/>\n"; // prints 21.5
15
16 ?>

                                 参数的默认值

                                在你期望参数具有支配地位的特定值的情况下,称为默认参数值(default argumentvalue)

1 <?php
2
3 function perform_sort($arrayData, $param2 = "qsort")
4 {
5 switch ($param)
6 {
7 case "qsort":
8 qsort($arrayData);
9 break;
10 case "insertion":
11 insertion_sort($arrayData);
12 break;
13 default:
14 bubble_sort($arrayData);
15 break;
16 }
17 }
18
19 ?>

                               可变数量的参数:

                                               php能够把任意数量的参数传递给函数,然后使用func_num_args、func_get_arg和func_get_args取得参数值

1 <?php
2
3 function print_parameter_values()
4 {
5 $all_parameters = func_get_args();
6
7 foreach ($all_parameters as $index => $value)
8 {
9 echo "Parameter $index has the value: $value<br/>\n";
10 }
11
12 echo "-----<br/>\n";
13 }
14
15 print_parameter_values(1, 2, 3, "fish");
16 print_parameter_values();
17
18 ?>

                                

      3.1.3 从函数返回值

                                 一些其他语言把在退出之前只执行一些代码的子例程和执行一引起代码并且把值返回调用者的函数区分开来,php和它们不同,所有php函数在返回调用者时

                                 都有一个值和它相关联。对于没有明确的返回值的函数,返回值为null

1 <?php
2
3 function does_nothing()
4 {
5 }
6
7 $ret = does_nothing();
8
9 echo '$ret: ' . (is_null($ret) ? '(null)' : $ret) . "<br/>";
10
11 ?>

                                 如果希望返回非null时,利用return把它和一个表达式关联

1 <?php
2
3 function is_even_number($number)
4 {
5 if (($number % 2) == 0)
6 return TRUE;
7 else
8 return FALSE;
9 }
10
11 ?>

                                   当你希望从函数返回多个值 时,把结果作为数组传递回来是方便的方式

                        

1 <?php
2
3 function get_user_name($userid)
4 {
5 //
6 // $all_user_data is a local variable (array) that temporarily
7 // holds all the information about a user.
8 //
9 $all_user_data = get_user_data_from_db($userid);
10
11 //
12 // after this function returns, $all_user_data no
13 // longer exists and has no value.
14 //
15 return $all_user_data["UserName"];
16 }
17
18 ?>

      3.1.4 函数内的变量范围

                                函数级别变量:

                                                  声明它们的函数内是合法,并且在函数的调用之间不记忆它们的值

1 <?php
2
3 $name = "Fatima";
4 echo "\$name: $name<br/>\n";
5
6 function set_name($new_name)
7 {
8 echo "\$name: $name<br/>\n";
9 $name = $new_name;
10 }
11
12 set_name("Giorgio");
13 echo "\$name: $name<br/>\n";
14
15 ?>

                              静态变量:

                                         static作为前缀的变量在函数调用之间保持它们的值不变,如果声明变量时为其赋值了,在运行当前脚本时,php只在第一次遇到这个变量时执行赋值

1 <?php
2 function increment_me()
3 {
4
5 // the value is set to 10 only once.
6 static $incr=10;
7
8 $incr++;
9 echo"$incr<br/>\n";
10
11 }
12 increment_me();
13 increment_me();
14 increment_me();
15 ?>
11
12
13

                                脚本内声明的变量("全局变量")

1 <?php
2
3 $name = "Fatima";
4 echo "\$name: $name<br/>\n";
5
6 function set_name($new_name)
7 {
8 echo "\$name: $name<br/>\n";
9 $name = $new_name;
10 }
11
12 set_name("Giorgio");
13 echo "\$name: $name<br/>\n";
14
15 ?>

l输出结果:

 $name: Fatima
$name:
$name: Fatima

                              如果在 内部组函数加一个globa ,那么输出结果

$name: Fatima
$name: Fatima
$name: Giorgio

                              

      3.1.5 函数范围和可用性

      3.1.6 把函数作为变量使用

        

1 <?php
2
3 function Log_to_File($message)
4 {
5 // open file and write message
6 }
7
8 function Log_to_Browser($message)
9 {
10 // output using echo or print functions
11 }
12
13 function Log_to_Network($message)
14 {
15 // connect to server and print message
16 }
17
18 //
19 // we're debugging now, so we'll just write to the screen
20 //
21 $log_type = "Log_to_Browser";
22
23 //
24 // now, throughout the rest of our code, we can just call
25 // $log_type(message) and change where it goes by simply
26 // changing the above variable assignment!
27 //
28 $log_type("beginning debug output");
29
30 ?>

                              但是php包含很多不能用作变量函数的语言结构,这种结构的明显例子是echo、print、var_dump、print_r、isset、unset、is_null is_type

3.2 中级代码重用:使用和包含文件

      3.2.1 把代码组织到文件中

                                       对通用功能进行分组: 如果希望把很多函数保存到单一位置上,典型情况是一个文件,即代码库(code library)

                                       生成一致的接口

1 <?php
2
3 // circle is (x, y) + radius
4 function compute_circle_area($x, $y, $radius)
5 {
6 return ($radius * pi() * pi());
7 }
8
9 function circle_move_location(&$y, &$x, $deltax, $deltay)
10 {
11 $x += $deltax;
12 $y += $deltay;
13 }
14
15 function compute_circumference_of_circle($radius)
16 {
17 return array("Circumference" => 2 * $radius * pi());
18 }
19
20 ?>

                                通过使用这此函数具有一致的名称、参数顺序以及返回值 ,可以显著地减少失败的可能性和代码中的缺陷。

1 <?php
2
3 //
4 // all routines in this file assume a circle is passed in as
5 // an array with:
6 // "X" => x coord "Y" => y coord "Radius" => circle radius
7 //
8
9 function circles_compute_area($circle)
10 {
11 return $circle["Radius"] * $circle["Radius"] * pi();
12 }
13
14 function circles_compute_circumference($circle)
15 {
16 return 2 * $circle["Radius"] * pi();
17 }
18
19 // $circle is passed in BY REFERENCE and modified!!!
20 function circles_move_circle(&$circle, $deltax, $deltay)
21 {
22 $circle["X"] += $deltax;
23 $circle["Y"] += $deltay;
24 }
25
26 ?>

      3.2.2 选择文件名和位置

                                  为了防止web用户打开.inc文件,我们使用两种机制防止这种情况发生,首先,在构成文档目录树中,我们确保web服务器不允许用户浏览或者加载

                                  不希望他们进行这些操作,在16章保护web应用程序中介绍,然后,然后将配置浏览器允许用户浏览.php和.html文件,但是不能浏览.inc文件

                                  防止这种问题的第二个途径不把代码入在文档树中,或存入其它目录,并且要么明确地在我们的代码中引用这个目录,通知php总是查看这个目录

      3.2.3 在脚本中包含库文件

                                  include 和require,这两个区别在于,当找不到文件时,require输出错误,而include输出警告。

                                 <?php

                                          include('i_dont_exit.inc');

                                          require('i_dont_exit.inc');\

                                  ?>

                                  include和require在哪里查找文件

                                  你可以指定明确的路经:

                                   require("/home/httpd/lib/frontend/table_gen.inc');

                                   require('http://www.cnblogs.com/lib/datafuncs.inc');

                                   require(d:\webapps\libs\data\connetions.inc');

                                   如果没有指定明确路径,php就在当前目录中查找要包含的文件,然后查找php.ini文件中的include_path设置中列出的目录.

                                  在windows是include_path=".;c:\php\include;d:\webapps\libs“设置完成后,不要忘记重新启动web服务器。

                                   include和require做了什么

                                   包含在脚本标记中的任何内容都作为一般 php脚本处理。

清单3-1和清单3-2显示php脚本和用于包含的简单文件

           清单3-1

      3.2.4 把包含用于页面模板化

1 <p align='center'>
2 <b>
3
4 <?php echo $message; ?>
5
6 </b>
7 </p>

           清单3-2            

1 <html>
2 <head>
3 <title>Sample</title>
4 </head>
5
6 <body>
7
8 <?php
9
10 $message = "Well, Howdy Pardner!";
11
12 include('printmessage.inc');
13
14 ?>
15
16
17 </body>
18
19 </html>

                                文件包含和函数范围    

                                               当把函数从脚本移动到包含文件时,会如何影响函数作用范围及调用它们的能力。

                                               如果一个函数在另一个文件中,并且这个文件没有通过include和require包含在当前脚本中,那么调用是非法的

                                               为了避免这个问题,在脚本开头包含其他文件是个好主意。

                               当共享变成问题时

                                                为了避免重复加载共享文件,可以用require_once()和include_once()语言结构防止函数或者结构重复定义的问题                                                                           

posted on 2011-05-26 09:57  平渡飞扬  阅读(454)  评论(0编辑  收藏  举报