I have a problem baffling me terribly. I noticed this before but didn t give it any heed until today.
I was trying to write my own check for integer strings. I know of is_numeric()
but it does not suffice since it counts float
as numeric not only integers
and is_int()
which does not work on string numbers.
I did something similar to this
$var1 = string ;
$var2 = 123 ;
var_dump( (int)$var1 == $var1);// boolean true
var_dump((int)$var2 == $var2);// boolean true
var_dump((int)$var1);//int 0
var_dump($var1);//string string (length=6)
正如第二批var倾销产出所预期的那样,因为我期望有php的松散比较表明,str和 in化版本是平等的。
然而,首先,我不了解为什么如此。 我已尝试用<条码>bool,但结果仍然给我。
我试图将 cast子分配到新的 v子里,对两者进行比较,结果仍然相同。
这是我错做的吗?
***Note
I am not comparing types here. I m actually trying to take advantage of the fact that int 0
is not equal to string string
.
我写道,我的愤怒检查方式不同,因此我实际上不需要其他选择。
***Edit
I did some extra checking and it turns out that 0 == string
is true as well. How is that possible?
***Edit 2 There are multiple correct answers below to the question. Thanks to everyone who answered.