11

This code:

var_dump(in_array("000", array(",00", ".00")));
var_dump(in_array("111", array(",11", ".11")));

output:

bool(true)
bool(false)

Why does the first line return true ?

sjagr
  • 15,983
  • 5
  • 40
  • 67
  • 1
    Could it have anything to do with [this behaviour](http://php.net/manual/en/function.in-array.php#91911)? – summea Feb 10 '15 at 17:07
  • 1
    I've edited your title. Please remember that "does not work" is a vague description of a problem for other people searching for the same problem! – sjagr Feb 10 '15 at 17:14

1 Answers1

10

It has to do with PHP's type coercion. The "000" essentially gets converted to just 0. To force it to use strict type checking, in_array() accepts a third parameter.

var_dump(in_array("000", array(",00", ".00"), true));

output:

bool(false)

EDIT: @andrekeller also pointed out the ".00" probably gets converted to int 0 as well. Moral of the story, don't trust PHP to get types right.

Cfreak
  • 19,191
  • 6
  • 49
  • 60