PHP中空字符串介绍0、null、empty和false之间的关系

0是数字,是empty,是false,不是null,值相当于空字符串,但类型不是字符串,去空格或强制转换为字符串型时不等于空字符串

""的值相当于0,是empty,是空字符串,是false,不是null,不是数字字符串

null的值相当于空字符串,值相当于0,是empty,是false,不是字符串,不是数字0

该值由字符串最前面的部分决定。如果字符串以合法的数字数据开始,就用该数字作为其值,否则其值为 0(零)。

"=="只要值相等就满足条件;  "==="需要两个变量的值和类型都相等;

strval();将变量转换为字符串类型;

intval();将变量转换为整型;

is_numeric();检测变量是否为数字或数字字符串,返回true或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) != '';"; 
} 

if(strval($a) == '')
{ 
    echo "strval(0) == '';"; 
}
else
{ 
    echo "strval(0) != '';"; 
} 
//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;"; 
}
else
{ 
    echo "0 is not numeric;"; 
} 
//0 is not 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;"; 
}


你可能感兴趣的:(php)