Re: in_array not operating as 'expected'
| From: | Ing. Ivo F.A.C. Fokkema | Date: | Tue, 09 Nov 2004 12:09:53 +0000 |
| Subject: | Re: in_array not operating as 'expected' | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-201615@lists.php.net to get a copy of this message | ||
Thanx both of you,
I know the type-casting bit and have noticed the third parameter which
'fixes' the in_array issue. I just missed 'test' == 0 implied (int)
'test'
== 0. Typecasting a string to integer indeed returned 0. Typecasting is
of course not necessary within if statements, resulting in my confusion.
Thanks again,
Ivo
On Tue, 09 Nov 2004 12:42:51 +0200, Bogdan Stancescu wrote:
> Apart from Marek's reply, you should probably check the in_array()
> documentation (look at the third, optional parameter) and search for type
> casting on php.net.
>
> Cheers,
> Bogdan
>
> Ing. Ivo F.A.C. Fokkema wrote:
>
>> Hi guys and gals,
>>
>> I'm not screaming "Bug! Bug!" but this _does_ look 'illogical' to
>> me.
>> I've searched the archives, but found no earlier conversation. Sorry if
>> I missed it. Consider the following code:
>>
>> var_dump(in_array('test', array(0)));
>>
>> What does this return? I expect bool(false), but it returns bool(true).
>> After some searching the web, I bumped into this:
>>
>> http://www.phpdiscuss.com/article.php?id=67763&group=php.bugs
>>
>> Basically, it is said by derick [at] php.net that this behavior is
>> expected. The following code :
>>
>> var_dump('test' == 0);
>>
>> also returns bool(true). But my logic tells me, that if 'test' == 0,
>> then :
>>
>> if (0) {
>> ...
>> }
>> }
>> should do the same as
>>
>> if ('test') {
>> ...
>> }
>> }
>> but it doesn't! The first if-statement is _not_ executed, the latter is.
>> In my opinion, this is not correct. Any thoughts on this? Am I not
>> seeing the logic here?
>>
>> Thanks for your thoughts.
>>
>> Ivo Fokkema