12

For this array ($options):

Array (
    [0] => 0
    [1] => 1
    [2] => 2
)

PHP returns TRUE:

$this->assertTrue( in_array('Bug', $options ) );         // TRUE
$this->assertTrue( in_array('Feature', $options ) );     // TRUE
$this->assertTrue( in_array('Task', $options ) );        // TRUE
$this->assertTrue( in_array('RockAndRoll', $options ) ); // TRUE  

Why?

Rizier123
  • 57,440
  • 16
  • 89
  • 140
Sebastian
  • 689
  • 1
  • 8
  • 15

3 Answers3

25

This is because 0 == "string" is true, and 0 is an element of the array.

Set the parameter $strict in in_array to true:

$this->assertTrue( in_array('Bug', $options, true) );
Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
6

Try adding a third parameter to your function calls;

$this->assertTrue( in_array('Bug', $options, true) ); 

This will ensure the comparisons are type-strict, and should solve your problem.

lynks
  • 5,453
  • 6
  • 22
  • 42
2

Add a third argument to in_array() and set it to TRUE.

alex
  • 460,746
  • 196
  • 858
  • 974