<meta charset="utf-8"> <?php // 一.isset() empty() is_null() 函数 /* 1.isset() 功能:检测变量是否设置和这个变量是否有具体的值 格式:bool isset(mixed $var [,mixed $var [,...]]) 返回值:true / false */ /* 2.is_null() 功能:检测变量是否未定义/未赋值/=null 都会返回null 格式:bool is_null(mixed $var) 返回值:true / false */ /* 3.empty() 功能:检测变量是否为空 变量不存在或者等于 0 "" null false 都返回true 格式:bool empty(mixed $var) 返回值:true / false */ error_reporting(E_ALL || ~E_NOTICE); //1.1变量为 NULL时 $var1 = NULL; echo '<br>'; $a = var_export(isset($var1),true); echo ("isset(\$var1=NULL)返回值:$a"); echo '<br>'; $a = var_export(is_null($var1),true); echo ("is_null(\$var1=NULL)返回值:$a"); echo '<br>'; $a = var_export(empty($var1),true); echo ("empty(\$var1=NULL)返回值:$a"); //1.2变量未定义时 echo "<hr>"; $a = var_export(isset($var2),true); echo ("isset(\$var2未定义)返回值:$a"); echo '<br>'; $a = var_export(is_null($var2),true); echo ("is_null(\$var2未定义)返回值:$a"); echo '<br>'; $a = var_export(empty($var2),true); echo ("empty(\$var2未定义)返回值:$a"); //1.3变量=''时 $var2 = ''; echo "<hr>"; $a = var_export(isset($var2),true); echo ("isset(\$var2='')返回值:$a"); echo '<br>'; $a = var_export(is_null($var2),true); echo ("is_null(\$var2='')返回值:$a"); echo '<br>'; $a = var_export(empty($var2),true); echo ("empty(\$var2='')返回值:$a"); //1.4变量='5'时 $var2 = '5'; echo "<hr>"; $a = var_export(isset($var2),true); echo ("isset(\$var2='5')返回值:$a"); echo '<br>'; $a = var_export(is_null($var2),true); echo ("is_null(\$var2='5')返回值:$a"); echo '<br>'; $a = var_export(empty($var2),true); echo ("empty(\$var2='5')返回值:$a"); //1.5变量=false时 $var2 = false; echo "<hr>"; $a = var_export(isset($var2),true); echo ("isset(\$var2=false)返回值:$a"); echo '<br>'; $a = var_export(is_null($var2),true); echo ("is_null(\$var2=false)返回值:$a"); echo '<br>'; $a = var_export(empty($var2),true); echo ("empty(\$var2=false)返回值:$a"); //1.6变量=true时 $var2 = true; echo "<hr>"; $a = var_export(isset($var2),true); echo ("isset(\$var2=true)返回值:$a"); echo '<br>'; $a = var_export(is_null($var2),true); echo ("is_null(\$var2=true)返回值:$a"); echo '<br>'; $a = var_export(empty($var2),true); echo ("empty(\$var2=true)返回值:$a"); //1.6变量=0时 $var2 = 0; echo "<hr>"; $a = var_export(isset($var2),true); echo ("isset(\$var2=0)返回值:$a"); echo '<br>'; $a = var_export(is_null($var2),true); echo ("is_null(\$var2=0)返回值:$a"); echo '<br>'; $a = var_export(empty($var2),true); echo ("empty(\$var2=0)返回值:$a"); //1.6变量=1时 $var2 = 1; echo "<hr>"; $a = var_export(isset($var2),true); echo ("isset(\$var2=1)返回值:$a"); echo '<br>'; $a = var_export(is_null($var2),true); echo ("is_null(\$var2=1)返回值:$a"); echo '<br>'; $a = var_export(empty($var2),true); echo ("empty(\$var2=1)返回值:$a"); // 二.变量的作用域 全局变量 局部变量 静态变量 echo '<hr>'; //全局变量 $x = 5; function test(){ $txt1 = 'hello php中文网!'; $y = 10; echo "局部变量\$txt1的值:$txt1"; echo '<br>'; echo "局部变量\$y的值:$y"; echo '<br>'; //使用全局变量 global $x; echo "全局变量\$x的值:$x"; } test(); echo '<br>'; //静态变量 function test2(){ static $i = 0; $i++; echo "静态变量\$i的值:$i"; echo '<br>'; if ($i < 5) { test2(); } } test2(); echo '<hr>'; ?>
点击 "运行实例" 按钮查看在线实例