php中0,null,empty,空,false,字符串关系的详细介绍

臭大佬 2017-09-15 14:46:21 1139
php 
简介 php中0,null,empty,空,false,字符串关系的详细介绍

// 判断 0 与 ''、null、empty、false 之间的关系   

$a = 0;

echo "0 与 ''、 empty、null、false 之间的关系:";


if ($a == '') {

    echo "0 == '';";//输出    

} else {

    echo "0 != '';";

}


if (trim($a) == '') {

    echo "trim(0) == '';";

} else {

    echo "trim(0) != '';"; //输出,因为trim(0)为字符串'0' 

}


if (strval($a) == '') {

    echo "strval(0) == '';";

} else {

    echo "strval(0) != '';"; //输出  strval — 获取变量的字符串值

}

//0=='',trim(0)!='',strval(0)!='' 不是空字符串  


if ($a === '') {

    echo "0 === '';";

} else {

    echo "0 !=== '';";  //输出 还比较类型 

}

//0!===''  


if (empty($a)) {

    echo "0 is empty;"; //输出  

} else {

    echo "0 is not empty;";

}

//0 is empty  


if (is_null($a)) {

    echo "0 is null;";

} else {

    echo "0 is not null;";  //输出 

}

//0 is not null  


if (is_numeric($a)) {

    echo "0 is numeric;"; //输出 如果$a='0',则结果相反  

} else {

    echo "0 is not numeric;";

}

//0 is numeric  


if (is_string($a)) {

    echo "0 is string;";

} else {

    echo "0 is not string;"; //输出   

}

//0 is not string  


if (!$a) {

    echo "0 is false;"; //输出  

} else {

    echo "0 is not false;";

}

//0 is false  


// 判断 '' 和 0、null、empty、false 之间的关系   

$a = '';

echo "'' 和 0、empty、null、false 之间的关系:";

if ($a == 0) {

    echo "'' == 0;";  //输出  

} else {

    echo "'' != 0;";

}


if (intval($a) == 0) {

    echo "intval('') == 0;";  //输出 

} else {

    echo "intval('') != 0;";

}


if (empty($a)) {

    echo "'' is empty;"; //输出   

} else {

    echo "'' is not empty;";

}


if (is_null($a)) {

    echo "'' is null;";

} else {

    echo "'' is not null;";//输出    

}


if (is_numeric($a)) {

    echo "'' is numeric;";

} else {

    echo "'' is not numeric;"; //输出  

}


if (is_string($a)) {

    echo "'' is string;"; //输出   

} else {

    echo "'' is not string;";

}


if (!$a) {

    echo "'' is false;"; //输出  

} else {

    echo "'' is not false;";

}


// 判断 null 和 ''、0、empty、false 之间的关系   

$a = null;

echo "null 和 ''、0、empty、false 之间的关系:";

if ($a == '') {

    echo "null == '';"; //输出  

} else {

    echo "null != '';";

}


if ($a == 0) {

    echo "null == 0;"; //输出  

} else {

    echo "null != 0;";

}


if ($a === '') {

    echo "null === '';";

} else {

    echo "null !=== '';"; //输出  

}


if ($a === 0) {

    echo "null === 0;";

} else {

    echo "null !=== 0;"; //输出  

}


if (strval($a) == '') {

    echo "strval(null) == '';"; //输出  

} else {

    echo "strval(null) != '';";

}


if (intval($a) == 0) {

    echo "intval(null) == 0;"; //输出  

} else {

    echo "intval(null) != 0;";

}


if (empty($a)) {

    echo "null is empty;"; //输出  

} else {

    echo "null is not empty;";

}


if (is_numeric($a)) {

    echo "null is numeric;";

} else {

    echo "null is not numeric;"; //输出  

}


if (is_string($a)) {

    echo "null is string;";

} else {

    echo "null is not string;"; //输出  

}


if (!$a) {

    echo "null is false;";

} else {

    echo "null is not false;"; //输出  

}