Annotation of embedaddon/php/ext/standard/tests/strings/implode1.phpt, revision 1.1.1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>