Matt
Matt

Reputation: 9433

What is the reason for this strange PHP behaviour?

I have the following code:

$data = array(); // prep array
$data['aardvark'] = true;

print_r($data); // output array
echo "\n";
var_dump(in_array('zebra', $data));

The output is as follows:

Array
(
    [aardvark] => 1
)

bool(true)

Despite the fact that zebra is clearly not in the array. It looks like it's to do with PHP's loose type system. (bool) 'zebra' is true, and there's a true in the array so the in_array returns true?

I think I can see the logic, but it's flawed. Is this a PHP bug?

Cheers.

Upvotes: 6

Views: 116

Answers (4)

codaddict
codaddict

Reputation: 454960

This happens because the string 'zebra' is non-empty and a non-empty string other than '0' is interpreted by PHP as true and since there is a matching value in your array you get true as result.

PHP did a conversion from string to boolean. To avoid this conversion you need to pass a third argument as true to in_array:

var_dump(in_array('zebra', $data, true));

Upvotes: 2

WWW
WWW

Reputation: 9860

"It's not a bug, it's a feature!" =)

Try doing in_array("zebra", $data, true); which will force "strict" checking (i.e. it will do a type-check on your variables).

Upvotes: 1

dqhendricks
dqhendricks

Reputation: 19251

Not a bug. You have it exactly right. To correctly find what you are looking for, you will have to do this:

if (in_array('zebra', $data, true)) {

Although this would probably be rare that you store different data types in the same array (strings and booleans). If you are storing data that are not a list, you should most likely be using an object instead.

Upvotes: 4

KingCrunch
KingCrunch

Reputation: 131841

You are right. in_array()

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

You must set the third parameter to true, if you want in_array() to test against the type too. Otherwise it will make loose comparison

var_dump(in_array('zebra', $data, true));

Usually this is no problem, because (in a clean design) usually all values are of the same type you know before you call in_array() and thus you can avoid calling it with mismatching types.

Upvotes: 3

Related Questions