array_slice_variation3.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. --TEST--
  2. Test array_slice() function : usage variations - Pass different data types as $length arg
  3. --SKIPIF--
  4. <?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
  5. --FILE--
  6. <?php
  7. /* Prototype : array array_slice(array $input, int $offset [, int $length [, bool $preserve_keys]])
  8. * Description: Returns elements specified by offset and length
  9. * Source code: ext/standard/array.c
  10. */
  11. /*
  12. * Pass different data types as $length argument to array_slice to test behaviour
  13. */
  14. echo "*** Testing array_slice() : usage variations ***\n";
  15. // Initialise function arguments not being substituted
  16. $input_array = array('one' => 1, 2, 'three' => 3, 4);
  17. $offset = 2;
  18. //get an unset variable
  19. $unset_var = 10;
  20. unset ($unset_var);
  21. // heredoc string
  22. $heredoc = <<<EOT
  23. hello world
  24. EOT;
  25. // unexpected values to be passed to $length argument
  26. $inputs = array(
  27. // int data
  28. /*1*/ 0,
  29. 1,
  30. 12345,
  31. -2345,
  32. // float data
  33. /*5*/ 10.5,
  34. -10.5,
  35. 12.3456789000e10,
  36. 12.3456789000E-10,
  37. .5,
  38. // null data
  39. /*10*/ NULL,
  40. null,
  41. // boolean data
  42. /*12*/ true,
  43. false,
  44. TRUE,
  45. FALSE,
  46. // empty data
  47. /*16*/ "",
  48. '',
  49. array(),
  50. // string data
  51. /*19*/ "string",
  52. 'string',
  53. $heredoc,
  54. // undefined data
  55. /*22*/ @$undefined_var,
  56. // unset data
  57. /*23*/ @$unset_var,
  58. );
  59. // loop through each element of $inputs to check the behavior of array_slice
  60. $iterator = 1;
  61. foreach($inputs as $input) {
  62. echo "\n-- Iteration $iterator --\n";
  63. var_dump( array_slice($input_array, $offset, $input) );
  64. $iterator++;
  65. };
  66. echo "Done";
  67. ?>
  68. --EXPECTF--
  69. *** Testing array_slice() : usage variations ***
  70. -- Iteration 1 --
  71. array(0) {
  72. }
  73. -- Iteration 2 --
  74. array(1) {
  75. ["three"]=>
  76. int(3)
  77. }
  78. -- Iteration 3 --
  79. array(2) {
  80. ["three"]=>
  81. int(3)
  82. [0]=>
  83. int(4)
  84. }
  85. -- Iteration 4 --
  86. array(0) {
  87. }
  88. -- Iteration 5 --
  89. array(2) {
  90. ["three"]=>
  91. int(3)
  92. [0]=>
  93. int(4)
  94. }
  95. -- Iteration 6 --
  96. array(0) {
  97. }
  98. -- Iteration 7 --
  99. array(2) {
  100. ["three"]=>
  101. int(3)
  102. [0]=>
  103. int(4)
  104. }
  105. -- Iteration 8 --
  106. array(0) {
  107. }
  108. -- Iteration 9 --
  109. array(0) {
  110. }
  111. -- Iteration 10 --
  112. array(2) {
  113. ["three"]=>
  114. int(3)
  115. [0]=>
  116. int(4)
  117. }
  118. -- Iteration 11 --
  119. array(2) {
  120. ["three"]=>
  121. int(3)
  122. [0]=>
  123. int(4)
  124. }
  125. -- Iteration 12 --
  126. array(1) {
  127. ["three"]=>
  128. int(3)
  129. }
  130. -- Iteration 13 --
  131. array(0) {
  132. }
  133. -- Iteration 14 --
  134. array(1) {
  135. ["three"]=>
  136. int(3)
  137. }
  138. -- Iteration 15 --
  139. array(0) {
  140. }
  141. -- Iteration 16 --
  142. array(0) {
  143. }
  144. -- Iteration 17 --
  145. array(0) {
  146. }
  147. -- Iteration 18 --
  148. array(0) {
  149. }
  150. -- Iteration 19 --
  151. array(0) {
  152. }
  153. -- Iteration 20 --
  154. array(0) {
  155. }
  156. -- Iteration 21 --
  157. array(0) {
  158. }
  159. -- Iteration 22 --
  160. array(2) {
  161. ["three"]=>
  162. int(3)
  163. [0]=>
  164. int(4)
  165. }
  166. -- Iteration 23 --
  167. array(2) {
  168. ["three"]=>
  169. int(3)
  170. [0]=>
  171. int(4)
  172. }
  173. Done