in_array_variation4.phpt 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. --TEST--
  2. Test in_array() function : usage variations - haystack as resource/multi dimentional array
  3. --FILE--
  4. <?php
  5. /*
  6. * Prototype : bool in_array ( mixed $needle, array $haystack [, bool $strict] )
  7. * Description: Searches haystack for needle and returns TRUE
  8. * if it is found in the array, FALSE otherwise.
  9. * Source Code: ext/standard/array.c
  10. */
  11. /* Test in_array() with haystack as resouce and multidimentional arrays */
  12. /* checking for Resources */
  13. echo "*** Testing resource type with in_array() ***\n";
  14. //file type resource
  15. $file_handle = fopen(__FILE__, "r");
  16. //directory type resource
  17. $dir_handle = opendir( dirname(__FILE__) );
  18. //store resources in array for comparison.
  19. $resources = array($file_handle, $dir_handle);
  20. // search for resouce type in the resource array
  21. var_dump( in_array($file_handle, $resources, true) );
  22. //checking for (int) type resource
  23. var_dump( in_array((int)$dir_handle, $resources, true) );
  24. /* Miscellenous input check */
  25. echo "\n*** Testing miscelleneos inputs with in_array() ***\n";
  26. //matching "Good" in array(0,"hello"), result:true in loose type check
  27. var_dump( in_array("Good", array(0,"hello")) );
  28. //false in strict mode
  29. var_dump( in_array("Good", array(0,"hello"), TRUE) );
  30. //matching integer 0 in array("this"), result:true in loose type check
  31. var_dump( in_array(0, array("this")) );
  32. // false in strict mode
  33. var_dump( in_array(0, array("this")),TRUE );
  34. //matching string "this" in array(0), result:true in loose type check
  35. var_dump( in_array("this", array(0)) );
  36. // false in stric mode
  37. var_dump( in_array("this", array(0), TRUE) );
  38. //checking for type FALSE in multidimensional array with loose checking, result:false in loose type check
  39. var_dump( in_array(FALSE,
  40. array("a"=> TRUE, "b"=> TRUE,
  41. array("c"=> TRUE, "d"=>TRUE)
  42. )
  43. )
  44. );
  45. //matching string having integer in beginning, result:true in loose type check
  46. var_dump( in_array('123abc', array(123)) );
  47. var_dump( in_array('123abc', array(123), TRUE) ); // false in strict mode
  48. echo "Done\n";
  49. ?>
  50. --EXPECTF--
  51. *** Testing resource type with in_array() ***
  52. bool(true)
  53. bool(false)
  54. *** Testing miscelleneos inputs with in_array() ***
  55. bool(true)
  56. bool(false)
  57. bool(true)
  58. bool(true)
  59. bool(true)
  60. bool(true)
  61. bool(false)
  62. bool(false)
  63. bool(true)
  64. bool(false)
  65. Done