implode1.phpt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. --TEST--
  2. Test implode() function
  3. --FILE--
  4. <?php
  5. /* Prototype: string implode ( string $glue, array $pieces );
  6. Description: Returns a string containing a string representation of all the
  7. array elements in the same order, with the glue string between each element.
  8. */
  9. echo "*** Testing implode() for basic opeartions ***\n";
  10. $arrays = array (
  11. array(1,2),
  12. array(1.1,2.2),
  13. array(array(2),array(1)),
  14. array(false,true),
  15. array(),
  16. array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc")
  17. );
  18. /* loop to output string with ', ' as $glue, using implode() */
  19. foreach ($arrays as $array) {
  20. var_dump( implode(', ', $array) );
  21. var_dump($array);
  22. }
  23. echo "\n*** Testing implode() with variations of glue ***\n";
  24. /* checking possible variations */
  25. $pieces = array (
  26. 2,
  27. 0,
  28. -639,
  29. true,
  30. "PHP",
  31. false,
  32. NULL,
  33. "",
  34. " ",
  35. "string\x00with\x00...\0"
  36. );
  37. $glues = array (
  38. "TRUE",
  39. true,
  40. false,
  41. array("key1", "key2"),
  42. "",
  43. " ",
  44. "string\x00between",
  45. NULL,
  46. -0,
  47. '\0'
  48. );
  49. /* loop through to display a string containing all the array $pieces in the same order,
  50. with the $glue string between each element */
  51. $counter = 1;
  52. foreach($glues as $glue) {
  53. echo "-- Iteration $counter --\n";
  54. var_dump( implode($glue, $pieces) );
  55. $counter++;
  56. }
  57. /* empty string */
  58. echo "\n*** Testing implode() on empty string ***\n";
  59. var_dump( implode("") );
  60. /* checking sub-arrays */
  61. echo "\n*** Testing implode() on sub-arrays ***\n";
  62. $sub_array = array(array(1,2,3,4), array(1 => "one", 2 => "two"), "PHP", 50);
  63. var_dump( implode("TEST", $sub_array) );
  64. var_dump( implode(array(1, 2, 3, 4), $sub_array) );
  65. var_dump( implode(2, $sub_array) );
  66. echo "\n*** Testing implode() on objects ***\n";
  67. /* checking on objects */
  68. class foo
  69. {
  70. function __toString() {
  71. return "Object";
  72. }
  73. }
  74. $obj = new foo(); //creating new object
  75. $arr = array();
  76. $arr[0] = &$obj;
  77. $arr[1] = &$obj;
  78. var_dump( implode(",", $arr) );
  79. var_dump($arr);
  80. /* Checking on resource types */
  81. echo "\n*** Testing end() on resource type ***\n";
  82. /* file type resource */
  83. $file_handle = fopen(__FILE__, "r");
  84. /* directory type resource */
  85. $dir_handle = opendir( dirname(__FILE__) );
  86. /* store resources in array for comparison */
  87. $resources = array($file_handle, $dir_handle);
  88. var_dump( implode("::", $resources) );
  89. echo "\n*** Testing error conditions ***\n";
  90. /* zero argument */
  91. var_dump( implode() );
  92. /* only glue */
  93. var_dump( implode("glue") );
  94. /* int as pieces */
  95. var_dump( implode("glue",1234) );
  96. /* NULL as pieces */
  97. var_dump( implode("glue", NULL) );
  98. /* pieces as NULL array */
  99. var_dump( implode(",", array(NULL)) );
  100. /* integer as glue */
  101. var_dump( implode(12, "pieces") );
  102. /* NULL as glue */
  103. var_dump( implode(NULL, "abcd") );
  104. /* args > than expected */
  105. var_dump( implode("glue", "pieces", "extra") );
  106. /* closing resource handles */
  107. fclose($file_handle);
  108. closedir($dir_handle);
  109. echo "Done\n";
  110. ?>
  111. --EXPECTF--
  112. *** Testing implode() for basic opeartions ***
  113. string(4) "1, 2"
  114. array(2) {
  115. [0]=>
  116. int(1)
  117. [1]=>
  118. int(2)
  119. }
  120. string(8) "1.1, 2.2"
  121. array(2) {
  122. [0]=>
  123. float(1.1)
  124. [1]=>
  125. float(2.2)
  126. }
  127. Notice: Array to string conversion in %s on line %d
  128. Notice: Array to string conversion in %s on line %d
  129. string(12) "Array, Array"
  130. array(2) {
  131. [0]=>
  132. array(1) {
  133. [0]=>
  134. int(2)
  135. }
  136. [1]=>
  137. array(1) {
  138. [0]=>
  139. int(1)
  140. }
  141. }
  142. string(3) ", 1"
  143. array(2) {
  144. [0]=>
  145. bool(false)
  146. [1]=>
  147. bool(true)
  148. }
  149. string(0) ""
  150. array(0) {
  151. }
  152. string(42) "a, aaaa, b, bbbb, c, ccccccccccccccccccccc"
  153. array(6) {
  154. [0]=>
  155. string(1) "a"
  156. [1]=>
  157. string(4) "aaaa"
  158. [2]=>
  159. string(1) "b"
  160. [3]=>
  161. string(4) "bbbb"
  162. [4]=>
  163. string(1) "c"
  164. [5]=>
  165. string(21) "ccccccccccccccccccccc"
  166. }
  167. *** Testing implode() with variations of glue ***
  168. -- Iteration 1 --
  169. string(63) "2TRUE0TRUE-639TRUE1TRUEPHPTRUETRUETRUETRUE TRUEstring�with�...�"
  170. -- Iteration 2 --
  171. string(36) "2101-639111PHP1111 1string�with�...�"
  172. -- Iteration 3 --
  173. string(27) "20-6391PHP string�with�...�"
  174. -- Iteration 4 --
  175. Notice: Array to string conversion in %s on line %d
  176. string(13) "key1Arraykey2"
  177. -- Iteration 5 --
  178. string(27) "20-6391PHP string�with�...�"
  179. -- Iteration 6 --
  180. string(36) "2 0 -639 1 PHP string�with�...�"
  181. -- Iteration 7 --
  182. string(153) "2string�between0string�between-639string�between1string�betweenPHPstring�betweenstring�betweenstring�betweenstring�between string�betweenstring�with�...�"
  183. -- Iteration 8 --
  184. string(27) "20-6391PHP string�with�...�"
  185. -- Iteration 9 --
  186. string(36) "2000-639010PHP0000 0string�with�...�"
  187. -- Iteration 10 --
  188. string(45) "2\00\0-639\01\0PHP\0\0\0\0 \0string�with�...�"
  189. *** Testing implode() on empty string ***
  190. Warning: implode(): Argument must be an array in %s on line %d
  191. NULL
  192. *** Testing implode() on sub-arrays ***
  193. Notice: Array to string conversion in %s on line %d
  194. Notice: Array to string conversion in %s on line %d
  195. string(27) "ArrayTESTArrayTESTPHPTEST50"
  196. Notice: Array to string conversion in %s on line %d
  197. string(19) "1Array2Array3Array4"
  198. Notice: Array to string conversion in %s on line %d
  199. Notice: Array to string conversion in %s on line %d
  200. string(18) "Array2Array2PHP250"
  201. *** Testing implode() on objects ***
  202. string(13) "Object,Object"
  203. array(2) {
  204. [0]=>
  205. &object(foo)#1 (0) {
  206. }
  207. [1]=>
  208. &object(foo)#1 (0) {
  209. }
  210. }
  211. *** Testing end() on resource type ***
  212. string(%d) "Resource id #%d::Resource id #%d"
  213. *** Testing error conditions ***
  214. Warning: implode() expects at least 1 parameter, 0 given in %s on line %d
  215. NULL
  216. Warning: implode(): Argument must be an array in %s on line %d
  217. NULL
  218. Warning: implode(): Invalid arguments passed in %s on line %d
  219. NULL
  220. Warning: implode(): Invalid arguments passed in %s on line %d
  221. NULL
  222. string(0) ""
  223. Warning: implode(): Invalid arguments passed in %s on line %d
  224. NULL
  225. Warning: implode(): Invalid arguments passed in %s on line %d
  226. NULL
  227. Warning: implode() expects at most 2 parameters, 3 given in %s on line %d
  228. NULL
  229. Done