PHP函数比较变量

作为一个PHPer,一定要多看PHP手册,每过一遍手册,你总会发现一些以前被你忽略的一些东西;
我们可以从各个不同的地方/渠道学习到PHP的相关知识,但也不要忘记我们的手册,PHP手册永远是学习和提升PHP的一本好书;

看到很多博文都有讨论过 ‘empty()的坑’、‘empty()与isset()的区别’ 之类 ,其实手册中很完整的给出了这些相关函数的注意点和区别

使用 PHP 函数对变量 $x 进行比较
表达式 gettype() empty() is_null() isset() boolean : if($x)
$x = ""; string TRUE FALSE TRUE FALSE
$x = null; NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x is undefined NULL TRUE TRUE FALSE FALSE
$x = array(); array TRUE FALSE TRUE FALSE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer FALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE
$x = -1; integer FALSE FALSE TRUE TRUE
$x = "1"; string FALSE FALSE TRUE TRUE
$x = "0"; string TRUE FALSE TRUE FALSE
$x = "-1"; string FALSE FALSE TRUE TRUE
$x = "php"; string FALSE FALSE TRUE TRUE
$x = "true"; string FALSE FALSE TRUE TRUE
$x = "false"; string FALSE FALSE TRUE TRUE

http://cn.php.net/manual/zh/types.comparisons.php

猜你喜欢

转载自www.cnblogs.com/cmnull/p/9921099.html
今日推荐