array_search_variation4.phpt 2.3 KB

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