<?php
header("content-type:text/html;charset=utf-8");
//变量的作用域
//php中,全局变量无法在定义的函数中直接使用
$i = 1;
function con($i){
// echo $i; //错误,$iunderfind
//如果想使用$i,
//第一种方法,使用$GLOBALS
echo $GLOBALS['i'];
//第二种,使用参数形式
echo $i;
}
echo con($i);
//php中局部变量在使用一次之后会自行销毁,如果想多次使用,在定义变量时使用static关键字定义
function test1(){
$a = 1;
echo $a;
$a++;
}
test1();//输出结果为1
echo '<br>';
test1();//输出结果为1
function test2(){
static $b = 1;
echo $b;
$b++;
}
test2();//输出结果为1
echo '<br>';
test2();//输出结果为2
//函数引用传递参数
$j = 1;
function test3(&$j){
$j++;
}
test3();
echo $j;
//递归
function DG($i){
if ($i == 1){
return 1;
}else if($i == 2){
return 1;
}else{
return DG($i-1) + DG($i-2);
}
echo DG(10);
}
?>