FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » why does in_array return true
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: why does in_array return true [message #169870 is a reply to message #169869] Wed, 29 September 2010 01:47 Go to previous messageGo to previous message
rf is currently offline  rf
Messages: 19
Registered: September 2010
Karma:
Junior Member
"heartmeat" <brendon(at)actrix(dot)co(dot)nz> wrote in message
news:d1fd2435-5e14-4d9f-93db-096bea63fbd7(at)p22g2000pre(dot)googlegroups(dot)com...
> hello all
>
> I must be missing something obvious, but, given:
>
> var $haystack = array(0);
> var $needle = '0_50';
>
> why does:
>
> in_array($needle,$haystack) return true?

This does not even parse. Unexpected 'var'.

Assuming what you meant is

$haystack = array(0);
$needle = '0_50';

then think about what in_array is doing internally. It's comparing the value
of $needle with each element of the array $haystack looking for a match.
It's a bit like

for ($i = 0; $i < count($haystack); $i++)
{
if ($needle == $haystack[$i])
return true;
}
return false;

Now, what is the result of ($needle == $haystack[0]) ?. True of course.

$haystack[0] is a number, so the comparison will cause $needle to be
converted to a number, along the lines of intval($needle). Now, $needle is
"0_50" and intval($needle) is, of course, 0. If $needle was "1_50" then
intval($needle) would be 1.

Did you perchance forget to include the third parameter of in_array, bool
$strict ? If you had then the above comparison would have been
if ($needle === $haystack[$i])
which of course false whenr $i is 0 as $needle and $haystack[0] are of
different type.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: PHP manual note
Next Topic: Out Of Memory with Memory_limit à -1
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 07:38:11 GMT 2024

Total time taken to generate the page: 0.04380 seconds