php bool false 跟 数值 0

WBOY
Release: 2016-06-13 11:01:05
Original
939 people have browsed it

php bool false 和 数值 0

转:http://home.phpchina.com/space.php?uid=108724&do=blog&id=54899


PHP 是类型宽松的语言,这意味着会 将布尔值 false 和整型值 0 视为相同(甚至 c 也把 0 和 false 视为等价)。可以使用一下脚本来确定 0 与布尔值?false 的等价性:

?

?$test_var = 0;
?if($test_var == false){
??echo "yes";
?}else{
??echo "no";
?}
?>?

?

输出:yes

?

_________________________________________________________________________________

?

?$test_var = 0;
?if($test_var !== false){
??echo "yes";
?}else{
??echo "no";
?}
?>

?

输出:yes

?

_________________________________________________________________

?

问题来了,布尔值 false 和整型值 0 明显是不同的。

?

?

PHP 有一个 gettype() 函数,得到变量的类型。可以使用 === 运算符(看好了,有三个等号)。和 == 运算符 的区别在于该运算符同时比较数据的值和类型

?

当终止条件中涉及不同的变量类型时,通过使用?===和?!==运算符进行强类型检查是很重要的。

?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!