字符串显示为等于数字 [英] String is being shown as equal to a number
问题描述
我的一些php出现了一个奇怪的问题.似乎正在发生的事情是显示0等于字符串"done".这是正在发生的事情:
I'm having a strange problem in some of my php. What seems to be happening is that 0 is being shown as equal to the string "done". Here's what's happening:
if(!isset($pointer)){
$pointer = 0;
}
error_log($pointer); //in this instance, I haven't set a pointer, returns 0
if($pointer == "done"){
die();
}
由于某种原因,第二个if语句将触发并杀死脚本.我不知道为什么,当$ pointer等于0时,它显然也等于"done".这是我所忽略的超级简单的事情吗?
For some reason, the second if statement is triggering and killing the script. I can't figure out why, when $pointer is equal to 0, it is apparently also equal to "done". Is this something super easy that I'm just overlooking?
我已经解决了这种情况,在第二个if语句上使用===给了我想要的结果,我只是想了解为什么它首先没有起作用.谢谢您的时间.
I've worked around the situation, using === on the second if statement gives me the desired result, I would just like to understand why it wasn't working in the first place. Thank you for your time.
推荐答案
这是类型比较问题;如您所说,使用:
It's a type comparison issue; as you say, using:
if($pointer === "done"){
强制检查值和类型.
正在发生的情况是,完成"被转换为数字以进行比较.由于那里没有数字,因此它为空.在比较中,null的值为0.
What is happening is that 'done' is being converted into a number for the comparison. Since there are no numbers in there, it's coming out as null. And null evaluates to 0 in your comparison.
这篇关于字符串显示为等于数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!