Home > Backend Development > PHP Tutorial > bool和 == 运算的有关问题

bool和 == 运算的有关问题

WBOY
Release: 2016-06-13 12:59:13
Original
1120 people have browsed it

bool和 == 运算的问题
在php官方的手册当中提供了这样一个例子:

<br />
Beware of certain control behavior with boolean and non boolean values :<br />
<br />
<?php<br />
// Consider that the 0 could by any parameters including itself<br />
var_dump(0 == 1); // false<br />
var_dump(0 == (bool)'all'); // false<br />
var_dump(0 == 'all'); // TRUE, take care<br />
var_dump(0 === 'all'); // false<br />
<br />
// To avoid this behavior, you need to cast your parameter as string like that :<br />
var_dump((string)0 == 'all'); // false<br />
?> <br />
Copy after login


不解为什么var_dump(0 == 'all'); 为true。使用 == 运算符是不是发生了类型转换?
但是为什么会相等……
------解决方案--------------------
是的,php做了数据类型转换
既然是比较大小,当然需要是同样的数据类型才能比较啦

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