PHP中空字符串介绍0、null、empty和false之间的关系_PHP教程
程序员文章站
2022-05-10 18:41:43
...
如果用错方法函数或是用少了,如果几个方法函数的逻辑顺序错了,很可能就是一个漏洞,而且不容易找出来。纠结啊~
// 判断 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) != '';";
}
if(strval($a) == ''){
echo "strval(0) == '';";
}else{
echo "strval(0) != '';";
}
if($a === ''){
echo "0 === '';";
}else{
echo "0 !=== '';";
}
if(empty($a)){
echo "0 is empty;";
}else{
echo "0 is not empty;";
}
if(is_null($a)){
echo "0 is null;";
}else{
echo "0 is not null;";
}
if(is_numeric($a)){
echo "0 is numeric;";
}else{
echo "0 is not numeric;";
}
if(is_string($a)){
echo "0 is string;";
}else{
echo "0 is not string;";
}
if(!$a){
echo "0 is false;";
}else{
echo "0 is not 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;";
}
echo "";
输出的结果是:
上网找找看哪位高人有总结出相关的结论出来,果然有!不过似乎不是很全面,我在这里稍完善了一下,仅供参考。
复制代码 代码如下:
// 判断 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) != '';";
}
if(strval($a) == ''){
echo "strval(0) == '';";
}else{
echo "strval(0) != '';";
}
if($a === ''){
echo "0 === '';";
}else{
echo "0 !=== '';";
}
if(empty($a)){
echo "0 is empty;";
}else{
echo "0 is not empty;";
}
if(is_null($a)){
echo "0 is null;";
}else{
echo "0 is not null;";
}
if(is_numeric($a)){
echo "0 is numeric;";
}else{
echo "0 is not numeric;";
}
if(is_string($a)){
echo "0 is string;";
}else{
echo "0 is not string;";
}
if(!$a){
echo "0 is false;";
}else{
echo "0 is not 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;";
}
echo "";
输出的结果是:
我想根据输出结果都可以清楚了解决空字符串、0、null、empty和false之间的大概关系,在开发过程中灵活运用上述代码里的方法,加上良好的逻辑,基本上应该是不会出现问题的了。
点击下载相关源代码
上一篇: SQL查询的几种方法总结
下一篇: 获取用户真实ip地址的方法
推荐阅读
-
PHP中空字符串介绍0、null、empty和false之间的关系
-
php中0,null,empty,空,false,字符串关系的详细介绍_PHP
-
PHP中空字符串、零、null、empty和false之间的关系
-
PHP中空字符串介绍0、null、empty和false之间的关系_PHP
-
PHP中空字符串介绍0、null、empty和false之间的关系_php技巧
-
深入理解php中0,null,empty,空,false,字符串间的关系
-
php中0,null,empty,空,false,字符串关系的详细介绍
-
PHP中空字符串介绍0、null、empty和false之间的关系_php技巧
-
PHP中空字符串介绍0、null、empty和false之间的关系_PHP教程
-
深入理解php中0,null,empty,空,false,字符串间的关系