-2
<?php

var_dump(in_array('ddds', array(2,3,0)));

Why this function return true? We have a string which could be converted to 1 but why to 0?

  • `in_array('ddds', array(2,3,0), TRUE)` returns false, properly, when you enforce strict checks. – Marc B Nov 26 '13 at 17:29

2 Answers2

4

Because 'ddds' is equal to 0 .

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

This is because ddds == 0 is true (demo), and 0 is an element of your array. Set the strict parameter for in_array() to ensure the types are also considered.

From the documentation:

If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

var_dump( in_array('ddds', array(2,3,0), TRUE) );

Output:

bool(false)
Amal Murali
  • 75,622
  • 18
  • 128
  • 150