array_unshift_variation8.phpt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. --TEST--
  2. Test array_unshift() function : usage variations - single quoted strings for 'var' argument
  3. --FILE--
  4. <?php
  5. /* Prototype : int array_unshift(array $array, mixed $var [, mixed ...])
  6. * Description: Pushes elements onto the beginning of the array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Testing the functionality of array_unshift() by passing different
  11. * single quoted strings for $var argument that is prepended to the array
  12. * passed through $array argument
  13. */
  14. echo "*** Testing array_unshift() : single quoted strings for \$var argument ***\n";
  15. // array to be passed to $array argument
  16. $array = array('f' => "first", "s" => 'second', 1, 2.222);
  17. // different variations of single quoted strings to be passed to $var argument
  18. $vars = array (
  19. '\$ -> This represents the dollar sign. hello dollar!!!',
  20. '\t\r\v The quick brown fo\fx jumped over the lazy dog',
  21. 'This is a text with special chars: \!\@\#\$\%\^\&\*\(\)\\',
  22. 'hello world\\t',
  23. 'This is \ta text in bold letters\r\s\malong with slashes\n : HELLO WORLD\t'
  24. );
  25. // loop through the various elements of $arrays to test array_unshift()
  26. $iterator = 1;
  27. foreach($vars as $var) {
  28. echo "-- Iteration $iterator --\n";
  29. $temp_array = $array; // assign $array to another temporary $temp_array
  30. /* with default argument */
  31. // returns element count in the resulting array after arguments are pushed to
  32. // beginning of the given array
  33. var_dump( array_unshift($temp_array, $var) );
  34. // dump the resulting array
  35. var_dump($temp_array);
  36. /* with optional arguments */
  37. // returns element count in the resulting array after arguments are pushed to
  38. // beginning of the given array
  39. $temp_array = $array;
  40. var_dump( array_unshift($temp_array, $var, "hello", 'world') );
  41. // dump the resulting array
  42. var_dump($temp_array);
  43. $iterator++;
  44. }
  45. echo "Done";
  46. ?>
  47. --EXPECTF--
  48. *** Testing array_unshift() : single quoted strings for $var argument ***
  49. -- Iteration 1 --
  50. int(5)
  51. array(5) {
  52. [0]=>
  53. string(54) "\$ -> This represents the dollar sign. hello dollar!!!"
  54. ["f"]=>
  55. string(5) "first"
  56. ["s"]=>
  57. string(6) "second"
  58. [1]=>
  59. int(1)
  60. [2]=>
  61. float(2.222)
  62. }
  63. int(7)
  64. array(7) {
  65. [0]=>
  66. string(54) "\$ -> This represents the dollar sign. hello dollar!!!"
  67. [1]=>
  68. string(5) "hello"
  69. [2]=>
  70. string(5) "world"
  71. ["f"]=>
  72. string(5) "first"
  73. ["s"]=>
  74. string(6) "second"
  75. [3]=>
  76. int(1)
  77. [4]=>
  78. float(2.222)
  79. }
  80. -- Iteration 2 --
  81. int(5)
  82. array(5) {
  83. [0]=>
  84. string(53) "\t\r\v The quick brown fo\fx jumped over the lazy dog"
  85. ["f"]=>
  86. string(5) "first"
  87. ["s"]=>
  88. string(6) "second"
  89. [1]=>
  90. int(1)
  91. [2]=>
  92. float(2.222)
  93. }
  94. int(7)
  95. array(7) {
  96. [0]=>
  97. string(53) "\t\r\v The quick brown fo\fx jumped over the lazy dog"
  98. [1]=>
  99. string(5) "hello"
  100. [2]=>
  101. string(5) "world"
  102. ["f"]=>
  103. string(5) "first"
  104. ["s"]=>
  105. string(6) "second"
  106. [3]=>
  107. int(1)
  108. [4]=>
  109. float(2.222)
  110. }
  111. -- Iteration 3 --
  112. int(5)
  113. array(5) {
  114. [0]=>
  115. string(56) "This is a text with special chars: \!\@\#\$\%\^\&\*\(\)\"
  116. ["f"]=>
  117. string(5) "first"
  118. ["s"]=>
  119. string(6) "second"
  120. [1]=>
  121. int(1)
  122. [2]=>
  123. float(2.222)
  124. }
  125. int(7)
  126. array(7) {
  127. [0]=>
  128. string(56) "This is a text with special chars: \!\@\#\$\%\^\&\*\(\)\"
  129. [1]=>
  130. string(5) "hello"
  131. [2]=>
  132. string(5) "world"
  133. ["f"]=>
  134. string(5) "first"
  135. ["s"]=>
  136. string(6) "second"
  137. [3]=>
  138. int(1)
  139. [4]=>
  140. float(2.222)
  141. }
  142. -- Iteration 4 --
  143. int(5)
  144. array(5) {
  145. [0]=>
  146. string(13) "hello world\t"
  147. ["f"]=>
  148. string(5) "first"
  149. ["s"]=>
  150. string(6) "second"
  151. [1]=>
  152. int(1)
  153. [2]=>
  154. float(2.222)
  155. }
  156. int(7)
  157. array(7) {
  158. [0]=>
  159. string(13) "hello world\t"
  160. [1]=>
  161. string(5) "hello"
  162. [2]=>
  163. string(5) "world"
  164. ["f"]=>
  165. string(5) "first"
  166. ["s"]=>
  167. string(6) "second"
  168. [3]=>
  169. int(1)
  170. [4]=>
  171. float(2.222)
  172. }
  173. -- Iteration 5 --
  174. int(5)
  175. array(5) {
  176. [0]=>
  177. string(74) "This is \ta text in bold letters\r\s\malong with slashes\n : HELLO WORLD\t"
  178. ["f"]=>
  179. string(5) "first"
  180. ["s"]=>
  181. string(6) "second"
  182. [1]=>
  183. int(1)
  184. [2]=>
  185. float(2.222)
  186. }
  187. int(7)
  188. array(7) {
  189. [0]=>
  190. string(74) "This is \ta text in bold letters\r\s\malong with slashes\n : HELLO WORLD\t"
  191. [1]=>
  192. string(5) "hello"
  193. [2]=>
  194. string(5) "world"
  195. ["f"]=>
  196. string(5) "first"
  197. ["s"]=>
  198. string(6) "second"
  199. [3]=>
  200. int(1)
  201. [4]=>
  202. float(2.222)
  203. }
  204. Done