开学了真的不想写博客了,几天憋出来一篇。不过每天学得到东西就好了。
一、变量函数
<?php
function show(){
echo "1111111111<br>";
}
$var = 'show';
$var();//1111111111
//利用var变量执行函数体内的内容
?>
二、可变个数参数的函数
<?php
function show(){
$arr = func_get_args();//得到函数的参数并以数组形式存储
echo "<pre>";
print_r($arr);
echo "</pre>";
//按原型格式输出数组的元素
$num = func_num_args();//得到数组元素的个数
echo $num;
//输出所含参数的个数
}
show(1,2,3,4,5);
?>
实例:用函数实现字符串的连接
<?php
function show(){
$arr = func_get_args();
foreach ($arr as $val) {
$str.=$val;
}//将$arr中的元素一个个取出送给$val
echo $str;
}
show('aaaaa','bbbbb','ccccc');//aaaaabbbbbccccc
?>
三、递归函数
<?php
$n=3;
$tot=0;
function sum($n){
global $tot;
$tot += $n;
if($n>1){
sum($n-1);
}
}
sum($n);
echo $tot;//6
?>
//实例实现数字累加
<?php
$n = 3;
function sum($n){
static $tot;
$tot += $n;
if($n>1){
sum($n-1);
}
return $tot;
}
echo sum($n);//6
?>
四、提高代码的重用性
将频繁使用的代码写成php文件,在php代码中进行引用
<body>
<div class="main">
<?php
require 'header1.php';
?>
</div>
<div class="content">
<?php
include 'content.php'
?>
</div>
</body>
1.include
2.require