第 7 章 使用函数

第 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 "&quot;$txt&quot;";'));
?>

结果:

<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>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容