PHP和Mysqlweb應(yīng)用開(kāi)發(fā)核心技術(shù) 第1部分 Php基礎(chǔ)-3 代碼組織和重用2
更新時(shí)間:2011年07月03日 23:41:46 作者:
創(chuàng)建可以調(diào)用的函數(shù)以便重用代碼把參數(shù)傳遞給函數(shù)并且從函數(shù)返回值和腳本的不同部分中的變量和數(shù)據(jù)進(jìn)行交互
從本章中,我們了解
.創(chuàng)建可以調(diào)用的函數(shù)以便重用代碼
.把參數(shù)傳遞給函數(shù)并且從函數(shù)返回值和腳本的不同部分中的變量和數(shù)據(jù)進(jìn)行交互
.把代碼和函數(shù)組存入到其他文件中,并且我們的腳本內(nèi)包含這些文件.
3.1基本代碼重用:函數(shù)
3.1.1 定義和調(diào)用函數(shù)
關(guān)鍵字function通知php這是一個(gè)函數(shù),后面跟著的是函數(shù)的名稱,它可以是字母、數(shù)字、字符或下劃線
函數(shù)名稱之后是參數(shù)列表,然后是函數(shù)體。在其它語(yǔ)言中名稱相同、但是參數(shù)列表不同的函數(shù),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 日本語(yǔ)のファンクション()
{
echo <<<EOT
Even Japanese characters are ok in function names, but be
extra careful with these (see Chapter 5).
EOT;
}
?>
3.1.2 把參數(shù)傳遞給函數(shù)
基本語(yǔ)法:為了把參數(shù)傳遞給函數(shù),在調(diào)用函數(shù)時(shí)需要把參數(shù)值 括在括號(hào)中,以逗號(hào)分隔。每個(gè)被傳遞的參數(shù)可
以是任何合法表達(dá)式,可以是變量、常量值、運(yùn)算符的結(jié)果,甚至可以是函數(shù)調(diào)用。
<?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);
?>
按引用傳遞:默認(rèn)情況下,只有變量的值被傳遞給函數(shù)。因此,對(duì)這個(gè)參數(shù)或者變量的任何改動(dòng)都只是在函數(shù)局部有效的
$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
如果你的目的是函數(shù)實(shí)際地修改傳遞給它的變量,而不僅僅處理其值的拷貝,那么可以用引用(reference)傳遞的功能。這是通過(guò)使用&字符完成的
<?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
?>
參數(shù)的默認(rèn)值
在你期望參數(shù)具有支配地位的特定值的情況下,稱為默認(rèn)參數(shù)值(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;
}
}
?>
可變數(shù)量的參數(shù):
php能夠把任意數(shù)量的參數(shù)傳遞給函數(shù),然后使用func_num_args、func_get_arg和func_get_args取得參數(shù)值
<?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();
?>
3.1.3 從函數(shù)返回值
一些其他語(yǔ)言把在退出之前只執(zhí)行一些代碼的子例程和執(zhí)行一引起代碼并且把值返回調(diào)用者的函數(shù)區(qū)分開(kāi)來(lái),php和它們不同,所有php函數(shù)在返回調(diào)用者時(shí)
都有一個(gè)值和它相關(guān)聯(lián)。對(duì)于沒(méi)有明確的返回值的函數(shù),返回值為null
<?php
function does_nothing()
{
}
$ret = does_nothing();
echo '$ret: ' . (is_null($ret) ? '(null)' : $ret) . "<br/>";
?>
如果希望返回非null時(shí),利用return把它和一個(gè)表達(dá)式關(guān)聯(lián)
<?php
function is_even_number($number)
{
if (($number % 2) == 0)
return TRUE;
else
return FALSE;
}
?>
當(dāng)你希望從函數(shù)返回多個(gè)值 時(shí),把結(jié)果作為數(shù)組傳遞回來(lái)是方便的方式
<?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 函數(shù)內(nèi)的變量范圍
函數(shù)級(jí)別變量:
聲明它們的函數(shù)內(nèi)是合法,并且在函數(shù)的調(diào)用之間不記憶它們的值
<?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";
?>
靜態(tài)變量:
static作為前綴的變量在函數(shù)調(diào)用之間保持它們的值不變,如果聲明變量時(shí)為其賦值了,在運(yùn)行當(dāng)前腳本時(shí),php只在第一次遇到這個(gè)變量時(shí)執(zhí)行賦值
<?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();
?>
腳本內(nèi)聲明的變量("全局變量")
<?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輸出結(jié)果:
$name: Fatima
$name:
$name: Fatima
如果在 內(nèi)部組函數(shù)加一個(gè)globa ,那么輸出結(jié)果
$name: Fatima
$name: Fatima
$name: Giorgio
3.1.5 函數(shù)范圍和可用性
3.1.6 把函數(shù)作為變量使用
<?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包含很多不能用作變量函數(shù)的語(yǔ)言結(jié)構(gòu),這種結(jié)構(gòu)的明顯例子是echo、print、var_dump、print_r、isset、unset、is_null is_type
3.2 中級(jí)代碼重用:使用和包含文件
3.2.1 把代碼組織到文件中
對(duì)通用功能進(jìn)行分組: 如果希望把很多函數(shù)保存到單一位置上,典型情況是一個(gè)文件,即代碼庫(kù)(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());
}
?>
通過(guò)使用這此函數(shù)具有一致的名稱、參數(shù)順序以及返回值 ,可以顯著地減少失敗的可能性和代碼中的缺陷。
<?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用戶打開(kāi).inc文件,我們使用兩種機(jī)制防止這種情況發(fā)生,首先,在構(gòu)成文檔目錄樹(shù)中,我們確保web服務(wù)器不允許用戶瀏覽或者加載
不希望他們進(jìn)行這些操作,在16章保護(hù)web應(yīng)用程序中介紹,然后,然后將配置瀏覽器允許用戶瀏覽.php和.html文件,但是不能瀏覽.inc文件
防止這種問(wèn)題的第二個(gè)途徑不把代碼入在文檔樹(shù)中,或存入其它目錄,并且要么明確地在我們的代碼中引用這個(gè)目錄,通知php總是查看這個(gè)目錄
3.2.3 在腳本中包含庫(kù)文件
include 和require,這兩個(gè)區(qū)別在于,當(dāng)找不到文件時(shí),require輸出錯(cuò)誤,而include輸出警告。
<?php
include('i_dont_exit.inc');
require('i_dont_exit.inc');\
?>
include和require在哪里查找文件
你可以指定明確的路經(jīng):
require("/home/httpd/lib/frontend/table_gen.inc');
require('http://www.cnblogs.com/lib/datafuncs.inc');
require(d:\webapps\libs\data\connetions.inc');
如果沒(méi)有指定明確路徑,php就在當(dāng)前目錄中查找要包含的文件,然后查找php.ini文件中的include_path設(shè)置中列出的目錄.
在windows是include_path=".;c:\php\include;d:\webapps\libs“設(shè)置完成后,不要忘記重新啟動(dòng)web服務(wù)器。
include和require做了什么
包含在腳本標(biāo)記中的任何內(nèi)容都作為一般 php腳本處理。
清單3-1和清單3-2顯示php腳本和用于包含的簡(jiǎn)單文件
清單3-1
3.2.4 把包含用于頁(yè)面模板化
<p align='center'>
<b>
<?php echo $message; ?>
</b>
</p>
清單3-2
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
$message = "Well, Howdy Pardner!";
include('printmessage.inc');
?>
</body>
</html>
文件包含和函數(shù)范圍
當(dāng)把函數(shù)從腳本移動(dòng)到包含文件時(shí),會(huì)如何影響函數(shù)作用范圍及調(diào)用它們的能力。
如果一個(gè)函數(shù)在另一個(gè)文件中,并且這個(gè)文件沒(méi)有通過(guò)include和require包含在當(dāng)前腳本中,那么調(diào)用是非法的
為了避免這個(gè)問(wèn)題,在腳本開(kāi)頭包含其他文件是個(gè)好主意。
當(dāng)共享變成問(wèn)題時(shí)
為了避免重復(fù)加載共享文件,可以用require_once()和include_once()語(yǔ)言結(jié)構(gòu)防止函數(shù)或者結(jié)構(gòu)重復(fù)定義的問(wèn)題
.創(chuàng)建可以調(diào)用的函數(shù)以便重用代碼
.把參數(shù)傳遞給函數(shù)并且從函數(shù)返回值和腳本的不同部分中的變量和數(shù)據(jù)進(jìn)行交互
.把代碼和函數(shù)組存入到其他文件中,并且我們的腳本內(nèi)包含這些文件.
3.1基本代碼重用:函數(shù)
3.1.1 定義和調(diào)用函數(shù)
關(guān)鍵字function通知php這是一個(gè)函數(shù),后面跟著的是函數(shù)的名稱,它可以是字母、數(shù)字、字符或下劃線
函數(shù)名稱之后是參數(shù)列表,然后是函數(shù)體。在其它語(yǔ)言中名稱相同、但是參數(shù)列表不同的函數(shù),php不支持這一特性。
復(fù)制代碼 代碼如下:
<?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 日本語(yǔ)のファンクション()
{
echo <<<EOT
Even Japanese characters are ok in function names, but be
extra careful with these (see Chapter 5).
EOT;
}
?>
3.1.2 把參數(shù)傳遞給函數(shù)
基本語(yǔ)法:為了把參數(shù)傳遞給函數(shù),在調(diào)用函數(shù)時(shí)需要把參數(shù)值 括在括號(hào)中,以逗號(hào)分隔。每個(gè)被傳遞的參數(shù)可
以是任何合法表達(dá)式,可以是變量、常量值、運(yùn)算符的結(jié)果,甚至可以是函數(shù)調(diào)用。
復(fù)制代碼 代碼如下:
<?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);
?>
按引用傳遞:默認(rèn)情況下,只有變量的值被傳遞給函數(shù)。因此,對(duì)這個(gè)參數(shù)或者變量的任何改動(dòng)都只是在函數(shù)局部有效的
復(fù)制代碼 代碼如下:
$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
如果你的目的是函數(shù)實(shí)際地修改傳遞給它的變量,而不僅僅處理其值的拷貝,那么可以用引用(reference)傳遞的功能。這是通過(guò)使用&字符完成的
復(fù)制代碼 代碼如下:
<?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
?>
參數(shù)的默認(rèn)值
在你期望參數(shù)具有支配地位的特定值的情況下,稱為默認(rèn)參數(shù)值(default argumentvalue)
復(fù)制代碼 代碼如下:
<?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;
}
}
?>
可變數(shù)量的參數(shù):
php能夠把任意數(shù)量的參數(shù)傳遞給函數(shù),然后使用func_num_args、func_get_arg和func_get_args取得參數(shù)值
復(fù)制代碼 代碼如下:
<?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();
?>
3.1.3 從函數(shù)返回值
一些其他語(yǔ)言把在退出之前只執(zhí)行一些代碼的子例程和執(zhí)行一引起代碼并且把值返回調(diào)用者的函數(shù)區(qū)分開(kāi)來(lái),php和它們不同,所有php函數(shù)在返回調(diào)用者時(shí)
都有一個(gè)值和它相關(guān)聯(lián)。對(duì)于沒(méi)有明確的返回值的函數(shù),返回值為null
復(fù)制代碼 代碼如下:
<?php
function does_nothing()
{
}
$ret = does_nothing();
echo '$ret: ' . (is_null($ret) ? '(null)' : $ret) . "<br/>";
?>
如果希望返回非null時(shí),利用return把它和一個(gè)表達(dá)式關(guān)聯(lián)
復(fù)制代碼 代碼如下:
<?php
function is_even_number($number)
{
if (($number % 2) == 0)
return TRUE;
else
return FALSE;
}
?>
當(dāng)你希望從函數(shù)返回多個(gè)值 時(shí),把結(jié)果作為數(shù)組傳遞回來(lái)是方便的方式
復(fù)制代碼 代碼如下:
<?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 函數(shù)內(nèi)的變量范圍
函數(shù)級(jí)別變量:
聲明它們的函數(shù)內(nèi)是合法,并且在函數(shù)的調(diào)用之間不記憶它們的值
復(fù)制代碼 代碼如下:
<?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";
?>
靜態(tài)變量:
static作為前綴的變量在函數(shù)調(diào)用之間保持它們的值不變,如果聲明變量時(shí)為其賦值了,在運(yùn)行當(dāng)前腳本時(shí),php只在第一次遇到這個(gè)變量時(shí)執(zhí)行賦值
復(fù)制代碼 代碼如下:
<?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();
?>
腳本內(nèi)聲明的變量("全局變量")
復(fù)制代碼 代碼如下:
<?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輸出結(jié)果:
$name: Fatima
$name:
$name: Fatima
如果在 內(nèi)部組函數(shù)加一個(gè)globa ,那么輸出結(jié)果
$name: Fatima
$name: Fatima
$name: Giorgio
3.1.5 函數(shù)范圍和可用性
3.1.6 把函數(shù)作為變量使用
復(fù)制代碼 代碼如下:
<?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包含很多不能用作變量函數(shù)的語(yǔ)言結(jié)構(gòu),這種結(jié)構(gòu)的明顯例子是echo、print、var_dump、print_r、isset、unset、is_null is_type
3.2 中級(jí)代碼重用:使用和包含文件
3.2.1 把代碼組織到文件中
對(duì)通用功能進(jìn)行分組: 如果希望把很多函數(shù)保存到單一位置上,典型情況是一個(gè)文件,即代碼庫(kù)(code library)
生成一致的接口
復(fù)制代碼 代碼如下:
<?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());
}
?>
通過(guò)使用這此函數(shù)具有一致的名稱、參數(shù)順序以及返回值 ,可以顯著地減少失敗的可能性和代碼中的缺陷。
復(fù)制代碼 代碼如下:
<?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用戶打開(kāi).inc文件,我們使用兩種機(jī)制防止這種情況發(fā)生,首先,在構(gòu)成文檔目錄樹(shù)中,我們確保web服務(wù)器不允許用戶瀏覽或者加載
不希望他們進(jìn)行這些操作,在16章保護(hù)web應(yīng)用程序中介紹,然后,然后將配置瀏覽器允許用戶瀏覽.php和.html文件,但是不能瀏覽.inc文件
防止這種問(wèn)題的第二個(gè)途徑不把代碼入在文檔樹(shù)中,或存入其它目錄,并且要么明確地在我們的代碼中引用這個(gè)目錄,通知php總是查看這個(gè)目錄
3.2.3 在腳本中包含庫(kù)文件
include 和require,這兩個(gè)區(qū)別在于,當(dāng)找不到文件時(shí),require輸出錯(cuò)誤,而include輸出警告。
復(fù)制代碼 代碼如下:
<?php
include('i_dont_exit.inc');
require('i_dont_exit.inc');\
?>
include和require在哪里查找文件
你可以指定明確的路經(jīng):
require("/home/httpd/lib/frontend/table_gen.inc');
require('http://www.cnblogs.com/lib/datafuncs.inc');
require(d:\webapps\libs\data\connetions.inc');
如果沒(méi)有指定明確路徑,php就在當(dāng)前目錄中查找要包含的文件,然后查找php.ini文件中的include_path設(shè)置中列出的目錄.
在windows是include_path=".;c:\php\include;d:\webapps\libs“設(shè)置完成后,不要忘記重新啟動(dòng)web服務(wù)器。
include和require做了什么
包含在腳本標(biāo)記中的任何內(nèi)容都作為一般 php腳本處理。
清單3-1和清單3-2顯示php腳本和用于包含的簡(jiǎn)單文件
清單3-1
3.2.4 把包含用于頁(yè)面模板化
<p align='center'>
<b>
<?php echo $message; ?>
</b>
</p>
清單3-2
復(fù)制代碼 代碼如下:
<html>
<head>
<title>Sample</title>
</head>
<body>
<?php
$message = "Well, Howdy Pardner!";
include('printmessage.inc');
?>
</body>
</html>
文件包含和函數(shù)范圍
當(dāng)把函數(shù)從腳本移動(dòng)到包含文件時(shí),會(huì)如何影響函數(shù)作用范圍及調(diào)用它們的能力。
如果一個(gè)函數(shù)在另一個(gè)文件中,并且這個(gè)文件沒(méi)有通過(guò)include和require包含在當(dāng)前腳本中,那么調(diào)用是非法的
為了避免這個(gè)問(wèn)題,在腳本開(kāi)頭包含其他文件是個(gè)好主意。
當(dāng)共享變成問(wèn)題時(shí)
為了避免重復(fù)加載共享文件,可以用require_once()和include_once()語(yǔ)言結(jié)構(gòu)防止函數(shù)或者結(jié)構(gòu)重復(fù)定義的問(wèn)題
您可能感興趣的文章:
- 如何使用jQuery+PHP+MySQL來(lái)實(shí)現(xiàn)一個(gè)在線測(cè)試項(xiàng)目
- php+mysql結(jié)合Ajax實(shí)現(xiàn)點(diǎn)贊功能完整實(shí)例
- PHP+MYSQL會(huì)員系統(tǒng)的開(kāi)發(fā)實(shí)例教程
- PHP MYSQL簡(jiǎn)易交互式站點(diǎn)開(kāi)發(fā)
- PHP和Mysqlweb應(yīng)用開(kāi)發(fā)核心技術(shù)-第1部分 Php基礎(chǔ)-2 php語(yǔ)言介紹
- PHP和Mysqlweb應(yīng)用開(kāi)發(fā)核心技術(shù) 第1部分 Php基礎(chǔ)-1 開(kāi)始了解php
- PHP與MySQL開(kāi)發(fā)的8個(gè)技巧小結(jié)
- php+mysql開(kāi)發(fā)的最簡(jiǎn)單在線題庫(kù)(在線做題系統(tǒng))完整案例
相關(guān)文章
模擬OICQ的實(shí)現(xiàn)思路和核心程序(三)
模擬OICQ的實(shí)現(xiàn)思路和核心程序(三)...2006-10-10桌面中心(一)創(chuàng)建數(shù)據(jù)庫(kù)
桌面中心(一)創(chuàng)建數(shù)據(jù)庫(kù)...2006-10-10php實(shí)現(xiàn)首頁(yè)自動(dòng)選擇語(yǔ)言轉(zhuǎn)跳
php實(shí)現(xiàn)首頁(yè)自動(dòng)選擇語(yǔ)言轉(zhuǎn)跳...2006-10-10php單例模式實(shí)現(xiàn)(對(duì)象只被創(chuàng)建一次)
這是我在php面試題中遇到的一道試題,單例模式按字面來(lái)看就是某一個(gè)類只有一個(gè)實(shí)例,這樣做的好處還是很大的,比如說(shuō)數(shù)據(jù)庫(kù)的連接,我們只需要實(shí)例化一次,不需要每次都去new了,這樣極大的降低了資源的耗費(fèi)2012-12-12