关于PHP中变量的判定
由于PHP解释性语言,所以一个变量即使没有定义也可以被使用而不会引起error。请看下面这个例子:<?phperror_reporting(E_ALL);ini_set('display_errors', '1');if(empty($test)) echo "empty<br/>";else echo "no empty<br/>";if(isset($test)) echo "isset<br/>";else echo "no set<br/>";if($test == null) echo "null<br/>";else echo "no null<br/>";if(defined($test)) echo "defined</br/>";else echo "no defined<br/>";switch ($test){case 0: echo "case 0"; break;case 1: echo "case 1"; break;}
输出的结果是:
empty
no set
Notice: Undefined variable: test in C:\AppServ\www\test2.php on line 11
null
Notice: Undefined variable: test in C:\AppServ\www\test2.php on line 14
no defined
Notice: Undefined variable: test in C:\AppServ\www\test2.php on line 18
case 0
有其实最后一个switch判断,这是一个比较隐晦的错误,所以在使用前进行一次判断还是有意义的。同时我们可以看到有些的判断方式会引起Notice而有些不会。至于判断的解读,请参考另一篇文章:http://blog.csdn.net/autofei/archive/2010/05/24/5619004.aspx
页:
[1]