implode1.phpt 6.0 KB

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