in_array_variation2.phpt 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. --TEST--
  2. Test in_array() function : usage variations - different haystack values
  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 different possible haystack values */
  12. echo "*** Testing in_array() with different haystack values ***\n";
  13. $misc_array = array (
  14. 'a',
  15. 'key' =>'d',
  16. 3,
  17. ".001" =>-67,
  18. "-.051" =>"k",
  19. 0.091 =>"-.08",
  20. "e" =>"5",
  21. "y" =>NULL,
  22. NULL =>"",
  23. 0,
  24. TRUE,
  25. FALSE,
  26. -27.39999999999,
  27. " ",
  28. "abcd\x00abcd\x00\abcd\x00abcdefghij",
  29. "abcd\nabcd\tabcd\rabcd\0abcd"
  30. );
  31. $array_type = array(TRUE, FALSE, 1, 0, -1, "1", "0", "-1", NULL, array(), "PHP", "");
  32. /* loop to do loose and strict type check of elements in
  33. $array_type on elements in $misc_array using in_array();
  34. checking PHP type comparison tables
  35. */
  36. $counter = 1;
  37. foreach($array_type as $type) {
  38. echo "-- Iteration $counter --\n";
  39. //loose type checking
  40. var_dump( in_array($type,$misc_array ) );
  41. //strict type checking
  42. var_dump( in_array($type,$misc_array,true) );
  43. //loose type checking
  44. var_dump( in_array($type,$misc_array,false) );
  45. $counter++;
  46. }
  47. echo "Done\n";
  48. ?>
  49. --EXPECT--
  50. *** Testing in_array() with different haystack values ***
  51. -- Iteration 1 --
  52. bool(true)
  53. bool(true)
  54. bool(true)
  55. -- Iteration 2 --
  56. bool(true)
  57. bool(true)
  58. bool(true)
  59. -- Iteration 3 --
  60. bool(true)
  61. bool(false)
  62. bool(true)
  63. -- Iteration 4 --
  64. bool(true)
  65. bool(true)
  66. bool(true)
  67. -- Iteration 5 --
  68. bool(true)
  69. bool(false)
  70. bool(true)
  71. -- Iteration 6 --
  72. bool(true)
  73. bool(false)
  74. bool(true)
  75. -- Iteration 7 --
  76. bool(true)
  77. bool(false)
  78. bool(true)
  79. -- Iteration 8 --
  80. bool(true)
  81. bool(false)
  82. bool(true)
  83. -- Iteration 9 --
  84. bool(true)
  85. bool(true)
  86. bool(true)
  87. -- Iteration 10 --
  88. bool(true)
  89. bool(false)
  90. bool(true)
  91. -- Iteration 11 --
  92. bool(true)
  93. bool(false)
  94. bool(true)
  95. -- Iteration 12 --
  96. bool(true)
  97. bool(true)
  98. bool(true)
  99. Done