第 7 章 使用函數
7.2 調用函數
調用內建的 abs()
函數
<?php
$num = -321;
$newnum = abs($num);
echo $newnum;
?>
結果:
321
7.3 定義一個函數
聲明并調用一個函數
<?php
function bighello()
{
echo "<h1>HELLO!</h1>";
}
bighello();
?>
結果:
<h1>HELLO!</h1>
聲明需要一個參數的函數
<?php
function printBR($txt) {
echo $txt."<br/>";
}
printBR("This is a line.");
printBR("This is a new line.");
printBR("This is yet another line.");
?>
結果:
This is a line.
This is a new line.
This is yet another line.
7.4 從用戶定義的函數返回值
返回一個值的函數
<?php
function addNums($firstNum, $secondNum) {
$result = $firstNum + $secondNum;
return $result;
}
echo addNums(3, 5);
?>
結果:
8
7.5 變量作用域
在一個函數中聲明的變量在函數外是不可用的
<?php
function test() {
$testvariable = "this is a test variable";
}
echo "test variable: ".$testvariable."<br/>";
?>
結果:
<br />
<b>Notice</b>: Undefined variable: testvariable in <b>/Applications/MAMP/htdocs/example.php</b> on line <b>5</b><br />
test variable:
在函數之外的變量不能在函數中訪問
<?php
$life = 42;
function meaningOfLife() {
echo "The meaning of life is ".$life;
}
meaningOfLife();
?>
結果:
<br />
<b>Notice</b>: Undefined variable: life in <b>/Applications/MAMP/htdocs/example.php</b> on line <b>4</b><br />
The meaning of life is
使用 global
語句訪問全局變量
<?php
$life = 42;
function meaningOfLife() {
global $life;
echo "The meaning of life is ".$life;
}
meaningOfLife();
?>
結果:
The meaning of life is 42
注意:參數就是調用代碼所傳遞的任何值的一個副本,在函數中修改它,對于函數塊以外的部分沒有任何影響。另一方面,在一個函數中修改一個全局變量,則會修改原始值而不是副本。
使用 global
語句在函數調用之間記錄一個變量的值
<?php
$num_of_calls = 0;
function numberedHeading($txt) {
global $num_of_calls;
$num_of_calls++;
echo "<h1>".$num_of_calls." ".$txt."</h1>";
}
numberedHeading("Widgets");
echo "<p>We build a fine range of widgets.</p>";
numberedHeading("Doodads");
echo "<p>Finest in the world.</p>";
?>
結果:
<h1>1 Widgets</h1><p>We build a fine range of widgets.</p><h1>2 Doodads</h1><p>Finest in the world.</p>
在一個函數中使用 static
語句聲明了一個變量,這個變量對于該函數仍保持為局部的,并且函數在從一次執行到另一次執行的過程中會“記住”該變量的值。
<?php
function numberedHeading($txt) {
static $num_of_calls;
$num_of_calls++;
echo "<h1>".$num_of_calls." ".$txt."</h1>";
}
numberedHeading("Widgets");
echo "<p>We build a fine range of widgets.</p>";
numberedHeading("Doodads");
echo "<p>Finest in the world.</p>";
?>
執行結果:同上
7.7 關于參數的更多內容
7.7.1 為參數設置默認值
帶有一個可選參數的函數
<?php
function fontwrap($txt, $fontsize="12pt") {
echo "<span style=\"font-size:$fontsize\">".$txt."</span>";
}
fontwrap("A Heading<br/>", "24pt");
fontwrap("some body text<br/>");
fontwrap("smaller body text<br/>");
fontwrap("even smaller body text<br/>");
?>
結果:
<span style="font-size:24pt">A Heading
</span><span style="font-size:12pt">some body text
</span><span style="font-size:12pt">smaller body text
</span><span style="font-size:12pt">even smaller body text
</span>
注意:當我們給一個可選參數一個默認值時,所有后續的參數也都應該給定默認值。
7.7.2 把變量引用傳遞給函數
值傳遞
<?php
function addFive($num) {
$num += 5;
}
$orignum = 10;
echo $orignum."<br/>";
addFive($orignum);
echo $orignum."<br/>";
?>
結果:
10
10
引用傳遞
<?php
function addFive(&$num) {
$num += 5;
}
$orignum = 10;
echo $orignum."<br/>";
addFive($orignum);
echo $orignum."<br/>";
?>
結果:
10
15
注意:在函數定義中的參數名的前面添加一個 &
符號,用引用來傳遞一個變量。
7.8 測試函數是否存在
可以使用 function_exists()
函數來檢查函數的可用性。function_exists()
需要一個表示函數名的字符串。如果可以找到該函數,它返回 true
,否則返回 false
。
<?php
function tagWrap($tag, $txt, $func="") {
if ( !empty($txt) && function_exists($func) ) {
$txt = $func($txt);
return "<$tag>$txt</$tag><br/>";
}
return "<strong>$txt</strong><br/>";
}
function underline($txt) {
return "<span style=\"text-decoration: underline;\">$txt</span>";
}
echo tagWrap('strong', 'make me bold');
echo tagWrap('em', 'underline and italicize me', 'underline');
echo tagWrap('em', 'make me italic and quote me', create_function('$txt', 'return ""$txt"";'));
?>
結果:
<strong>make me bold</strong>
<em><span style="text-decoration: underline;">underline and italicize me</span></em>
<em>"make me italic and quote me"</em>