Home > Backend Development > PHP Tutorial > PHP字符串"all"和整型0比较的奇怪问题

PHP字符串"all"和整型0比较的奇怪问题

WBOY
Release: 2016-06-23 14:01:29
Original
845 people have browsed it

<?php$a = "all";$b = 0;var_dump($a);var_dump($b);$cmp = ($a==$b);var_dump($cmp);			//bool(true)???$cmp2 = ($a===$b);var_dump($cmp2);		//bool(false)
Copy after login


以上问题何解?


回复讨论(解决方案)

任何类型和数值类型与做比较时,都将化为数值类型进行比较
"all" 化为数值就是 0 了,所以 bool(true)

一个例外的是 包含类型的比较 ===
他是先做类型比较,在做值比较

不同变量类型进行比较的时候,PHP有内部机制进行转换,再加以比较。

$cmp = (intval($a)==$b);
Copy after login

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