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 ?

2
  • 1
    Could it have anything to do with this behaviour? Commented Feb 10, 2015 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! Commented Feb 10, 2015 at 17:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I would have written something similar, +1
agreed, deleted my similar answer
@andrekeller was going to upvote you too. You're answer is also correct as it mentions the .00 being converted as well. Knowing PHP its probably both
@Cfreak: well then update your answer, does not make sense have to almost identical answers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.