实例
<?php header("Content-type: text/html; charset=utf-8"); echo "<h2>变量的作用域与检测方法</h2>"; echo "<hr style='border:2px solid red'>"; //is_null echo "<h3>is_null()</h3>"; $name=null;//变量值为null直接返回true var_dump(is_null($name)); $name1;//只声明变量名,不赋值,返回true,报警 @var_dump(is_null($name1)); $name2='hello!'; var_dump(is_null($name2)); unset($name2);//破坏掉已赋值变量,返回true,报警 @var_dump(is_null($name2)); //empty echo "<h3>empty()</h3>"; $user1='';//变量赋值为空,返回true var_dump(empty($user1)); $user2='0';//变量赋值为字符串'0',返回true var_dump(empty($user2)); $user3=0;//变量赋值为0,返回true var_dump(empty($user3)); $user4=null;//变量赋值为null,返回true var_dump(empty($user4)); $user5=[];//变量赋值为空数组,返回true var_dump(empty($user5)); $user6=false;//变量赋值为false,返回true var_dump(empty($user6)); //isset() echo "<h3>isset()</h3>"; $answ1='';//变量为空值,返回true var_dump(isset($answ1)); $answ2='0';//变量有赋值,返回true var_dump(isset($answ2)); $answ3=false;//变量有赋值false,返回true var_dump(isset($answ3)); $answ4=null;//变量声明值为null,返回false var_dump(isset($answ4)); $answ5=[];//变量有赋值空数组,返回true var_dump(isset($answ5)); $answ6;//变量声明后无赋值,返回false var_dump(isset($answ6)); //变量未声明 var_dump(isset($answ7));
运行实例 »
点击 "运行实例" 按钮查看在线实例