函数就是可以完成某个工作的代码块,在使用的时候,拿来即用,而不用考虑它的内部组成。PHP函数可以分为两类,一类是内置函数,即PHP自身的函数,只需要根据函数名调用即可。另一类是自定义函数,就是由用户自己定义的、用来实现特定功能的函数。
定义和调用函数
创建函数的基本语法格式为:
<?php
function fun_name($str1,$str2,$strn){
fun_body;
}
?>
参数说明如下:
function
:为声明自定义函数时必须使用到的关键字fun_name
:为自定义函数的名称$str1 ... $strn
:为函数的参数fun_body
:为自定义函数的主体,是功能实现部分
当函数被定义好后,,所要做的就是调用这个函数。调用函数的操作十分简单,只需要引用函数名并赋予正确的参数即可完成函数的调用。
代码示例:
<?php
// 声明自定义函数
function example($num){
echo "$num * $num = ".$num * $num;
}
example(10);
?>
运行结果:
10 * 10 = 100;
在函数间传递参数
参数传递的方法有按值传递、按引用传递和默认参数三种。
按值传递方式
按值传递方式是最常用的的参数传递方式,即将调用者括号内的值依次传递给函数括号内的值。
代码示例:
<?php # 按值传递 function test($num1,$num2,$num3){ echo '$num1 是:'.$num1."<br>"; $num1++; echo '$num2 是:'.$num2."<br>"; echo '$num3 是:'.$num3."<br>"; }
<span class="token variable">$num1</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token variable">$num2</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token variable">$num3</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span> <span class="token shell-comment comment"># 调用自定义函数</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token variable">$num1</span><span class="token punctuation">,</span><span class="token variable">$num2</span><span class="token punctuation">,</span><span class="token variable">$num3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token double-quoted-string string">"<br>"</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token single-quoted-string string">'$num1 是:'</span><span class="token punctuation">.</span><span class="token variable">$num1</span><span class="token punctuation">.</span><span class="token double-quoted-string string">"<br>"</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token single-quoted-string string">'$num2 是:'</span><span class="token punctuation">.</span><span class="token variable">$num2</span><span class="token punctuation">.</span><span class="token double-quoted-string string">"<br>"</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token single-quoted-string string">'$num3 是:'</span><span class="token punctuation">.</span><span class="token variable">$num3</span><span class="token punctuation">.</span><span class="token double-quoted-string string">"<br>"</span><span class="token punctuation">;</span>
?>
运行结果:
$num1 是:1 $num2 是:2 $num3 是:3
$num1 是:1
$num2 是:2
$num3 是:3
按引用传递方式
按引用传递就是将参数的内存地址传递到函数中。这时,在函数内部的所有操作都会影响到调用者参数的值。使用引用传递方式传值时只需在源基础上加”&“即可。
代码示例:
<?php # 按引用传递 function test(&$num1,$num2,$num3){ echo '$num1 是:'.$num1."<br>"; $num1++; echo '$num2 是:'.$num2."<br>"; echo '$num3 是:'.$num3."<br>"; }
<span class="token variable">$num1</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token variable">$num2</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token variable">$num3</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span> <span class="token shell-comment comment"># 调用自定义函数</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token variable">$num1</span><span class="token punctuation">,</span><span class="token variable">$num2</span><span class="token punctuation">,</span><span class="token variable">$num3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token double-quoted-string string">"<br>"</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token single-quoted-string string">'$num1 是:'</span><span class="token punctuation">.</span><span class="token variable">$num1</span><span class="token punctuation">.</span><span class="token double-quoted-string string">"<br>"</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token single-quoted-string string">'$num2 是:'</span><span class="token punctuation">.</span><span class="token variable">$num2</span><span class="token punctuation">.</span><span class="token double-quoted-string string">"<br>"</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token single-quoted-string string">'$num3 是:'</span><span class="token punctuation">.</span><span class="token variable">$num3</span><span class="token punctuation">.</span><span class="token double-quoted-string string">"<br>"</span><span class="token punctuation">;</span>
?>
运行结果:
$num1 是:1 $num2 是:2 $num3 是:3
$num1 是:2
$num2 是:2
$num3 是:3
默认参数(可选参数)
默认参数也称可选参数,可选参数与必选参数的区别在于,声明函数参数时,需要为可选参数赋值;在调用函数时,可选参数可以赋值也可以不赋值,若不赋值,则参数使用声明时的值,若赋值,则参数使用新赋的值。
代码示例:
<?php # 可选参数代码示例 function test($price,$tax = 0){ $price = $price + ($price * $tax); echo "价格:$price<br>"; }
<span class="token shell-comment comment"># 调用自定义函数</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token number">0.25</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
?>
运行结果:
价格:125
价格:100
从函数中返回值
调用函数时可以向函数发送信息(参数),函数也可以向调用者发回信息(返回值)。从函数返回的值称为结果(result)或返回值(return value)。函数将返回值传递给调用者的方式是使用关键字return。return将函数的返回值返回给函数的调用者,即将程序的控制权返回到调用者的作用域。
代码示例:
<?php # 定义total()函数,计算商品总价 function total($price,$number){ $total = $price * $number; return $total; }
<span class="token variable">$sum</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token variable">$phone</span> <span class="token operator">=</span> <span class="token function">total</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$computer</span> <span class="token operator">=</span> <span class="token function">total</span><span class="token punctuation">(</span><span class="token number">8000</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$sum</span> <span class="token operator">=</span> <span class="token variable">$phone</span> <span class="token operator">+</span> <span class="token variable">$computer</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token double-quoted-string string">"合计"</span><span class="token punctuation">.</span><span class="token variable">$sum</span><span class="token punctuation">.</span><span class="token double-quoted-string string">"元"</span><span class="token punctuation">;</span>
?>
运行结果:
合计90000元
注:
- return语句只能返回一个参数,即只能返回一个值,不能一次返回多个值。如果要返回多个结果,就要在函数中定义一个数组,将返回值存储在数组中返回。
变量作用域
在编写代码时,虽然有些变量在函数之外,有些变量在函数之内,但它们必须在有效范围内使用,如果变量超出有效范围,变量也就失去其意义。
作用域 | 说明 |
---|---|
局部变量 | 在函数的内部定义的变量,其作用域是所在函数 |
全局变量 | 被定义在所有函数之外的变量,其作用域是整个PHP文件,但在用户自定义函数内部是不可用的。如果希望在用户自定义函数内部使用全局变量,则要使用global关键字声明 |
静态变量 | 能够在函数调用结束后仍保留变量值,当再次回到其作用域时,又可以继续使用原来的值。而一般变量在函数调用结束后,其存储的数据值将被清除,所占的内存空间被释放。使用讲台变量时,先要用关键字static来声明变量,把关键字static放在要定义的变量之前 |
代码示例:
<?php $example = "在...函数外"; function example(){ $example = "...在函数内..."; echo "在函数内输出的内容是:$example.<br>"; }
<span class="token function">example</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token double-quoted-string string">"在函数外输出的内容是:<span class="token interpolation"><span class="token variable">$example</span></span>.<br>"</span><span class="token punctuation">;</span>
?>
运行结果:
在函数内输出的内容是:...在函数内....
在函数外输出的内容是:在...函数外.