Home > Backend Development > PHP Tutorial > 这样也行?该怎么解决

这样也行?该怎么解决

WBOY
Release: 2016-06-13 13:29:26
Original
866 people have browsed it

这样也行?
所有字母的字符串 和 0 都是恒等的?求解释

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
if("e" == 0){
    echo "Yes";
}
#result:
#Yes

Copy after login


------解决方案--------------------
"e" 不是变量 在内存中什么都没有.......
------解决方案--------------------
this way :
PHP code
echo "e" === 0;
<br><font color="#e78608">------解决方案--------------------</font><br>php会自动转换变量的类型,int和string对比那么会将string转成int,如此一来所有字符串都是0,那么结果也就确定了,你可以用===全等来判断
<br><font color="#e78608">------解决方案--------------------</font><br>这样解决更清楚:<br><br>echo intval('e');#转换不成功,默认0(print:0)<br>echo intval('1ds2e');#字符串中有整数出现,则取第一次出现的位置,转换成int (print:1)<br><br>
Copy after login
探讨

this way : PHP code
echo "e" === 0;

------解决方案--------------------

这句说法是错的,“字符串中有整数出现,则取第一次出现的位置,转换成int (print:1) ”

应该是这样:

字符串中有整数出现,并且整数在字符串的起始位置,则取出,转换成int

探讨

这样解决更清楚:

echo intval('e');#转换不成功,默认0(print:0)
echo intval('1ds2e');#字符串中有整数出现,则取第一次出现的位置,转换成int (print:1)

引用:

this way : PHP code
echo "e" === 0;
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