Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in_array function return TRUE when we are looking for an empty string? [duplicate]

Tags:

php

Could someone explain to me why that is true?

in_array('', array(0,1,2));
like image 572
Broda Noel Avatar asked Dec 19 '22 08:12

Broda Noel


1 Answers

Because, as said in the docs:

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

Searches haystack for needle using loose comparison unless strict is set.

... and '' == 0 is true in PHP. If you want to use strict comparison, just call in_array() with three params:

in_array('', array(0, 1, 2), true); // false

... so the types will be checked as well, and String '' won't have a chance to match against Numbers.

like image 128
raina77ow Avatar answered Dec 24 '22 01:12

raina77ow