1

I found a strange error in in_array() PHP function.

Next code returns TRUE but it's wrong value. Anyone can told my what I do wrong?

<?php
var_dump(in_array(0, array('a', 'ab', 'abc')));

Live demo: http://sandbox.onlinephpfunctions.com/code/81013d156637fd06557b632f102d52f72d0f9d77

Of course I know that I can execute in_array(0, array('a', 'ab', 'abc'), true) but why without this true this function isn't working good.

ventaquil
  • 2,780
  • 3
  • 23
  • 48

2 Answers2

2

You have to set it strict

var_dump(in_array(0, array('a', 'ab', 'abc'), true));
VikingBlooded
  • 884
  • 1
  • 6
  • 17
1

in_array you have to set type

Syntax:

in_array(search,array,type);

var_dump(in_array(0, array('a', 'ab', 'abc'), true));

Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

http://php.net/manual/en/function.in-array.php

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46