php-src/ext/standard/tests/strings/implode1.phpt

412 lines
8.6 KiB
Plaintext
Raw Normal View History

2006-08-22 19:51:34 +00:00
--TEST--
Test implode() function
2006-08-22 19:51:34 +00:00
--FILE--
<?php
/* Prototype: string implode ( string $glue, array $pieces );
Description: Returns a string containing a string representation of all the
array elements in the same order, with the glue string between each element.
*/
echo "*** Testing implode() for basic opeartions ***\n";
$arrays = array (
array(1,2),
array(1.1,2.2),
array(array(2),array(1)),
array(false,true),
array(),
array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc")
);
/* loop to output string with ', ' as $glue, using implode() */
foreach ($arrays as $array) {
var_dump( implode(', ', $array) );
var_dump($array);
}
echo "\n*** Testing implode() with variations of glue ***\n";
/* checking possible variations */
$pieces = array (
2,
0,
-639,
true,
"PHP",
false,
NULL,
"",
" ",
"string\x00with\x00...\0"
);
$glues = array (
"TRUE",
true,
false,
array("key1", "key2"),
"",
" ",
"string\x00between",
NULL,
-0,
'\0'
);
/* loop through to display a string containing all the array $pieces in the same order,
with the $glue string between each element */
$counter = 1;
foreach($glues as $glue) {
echo "-- Iteration $counter --\n";
var_dump( implode($glue, $pieces) );
$counter++;
}
/* empty string */
echo "\n*** Testing implode() on empty string ***\n";
var_dump( implode("") );
2006-08-22 19:51:34 +00:00
/* checking sub-arrays */
echo "\n*** Testing implode() on sub-arrays ***\n";
$sub_array = array(array(1,2,3,4), array(1 => "one", 2 => "two"), "PHP", 50);
var_dump( implode("TEST", $sub_array) );
var_dump( implode(array(1, 2, 3, 4), $sub_array) );
var_dump( implode(2, $sub_array) );
2006-08-22 19:51:34 +00:00
echo "\n*** Testing implode() on objects ***\n";
/* checking on objects */
class foo
{
function __toString() {
return "Object";
}
2006-08-22 19:51:34 +00:00
}
$obj = new foo(); //creating new object
$arr = array();
$arr[0] = &$obj;
$arr[1] = &$obj;
var_dump( implode(",", $arr) );
var_dump($arr);
2007-05-14 13:05:29 +00:00
/* Checking on resource types */
echo "\n*** Testing end() on resource type ***\n";
/* file type resource */
$file_handle = fopen(__FILE__, "r");
/* directory type resource */
$dir_handle = opendir( dirname(__FILE__) );
/* store resources in array for comparision */
$resources = array($file_handle, $dir_handle);
var_dump( implode("::", $resources) );
echo "\n*** Testing error conditions ***\n";
/* zero argument */
var_dump( implode() );
/* only glue */
var_dump( implode("glue") );
/* int as pieces */
var_dump( implode("glue",1234) );
/* NULL as pieces */
var_dump( implode("glue", NULL) );
/* pieces as NULL array */
var_dump( implode(",", array(NULL)) );
/* integer as glue */
var_dump( implode(12, "pieces") );
/* NULL as glue */
var_dump( implode(NULL, "abcd") );
/* args > than expected */
var_dump( implode("glue", "pieces", "extra") );
/* closing resource handles */
2007-05-14 13:05:29 +00:00
fclose($file_handle);
closedir($dir_handle);
2006-08-22 19:51:34 +00:00
echo "Done\n";
?>
--EXPECTF--
*** Testing implode() for basic opeartions ***
2006-08-22 19:51:34 +00:00
string(4) "1, 2"
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
string(8) "1.1, 2.2"
array(2) {
[0]=>
float(1.1)
[1]=>
float(2.2)
}
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
string(12) "Array, Array"
array(2) {
[0]=>
array(1) {
[0]=>
int(2)
}
[1]=>
array(1) {
[0]=>
int(1)
}
}
string(3) ", 1"
array(2) {
[0]=>
bool(false)
[1]=>
bool(true)
}
string(0) ""
array(0) {
2006-08-22 19:51:34 +00:00
}
string(42) "a, aaaa, b, bbbb, c, ccccccccccccccccccccc"
array(6) {
2006-08-22 19:51:34 +00:00
[0]=>
string(1) "a"
2006-08-22 19:51:34 +00:00
[1]=>
string(4) "aaaa"
[2]=>
string(1) "b"
[3]=>
string(4) "bbbb"
[4]=>
string(1) "c"
[5]=>
string(21) "ccccccccccccccccccccc"
2006-08-22 19:51:34 +00:00
}
*** Testing implode() with variations of glue ***
-- Iteration 1 --
2007-05-14 13:05:29 +00:00
string(63) "2TRUE0TRUE-639TRUE1TRUEPHPTRUETRUETRUETRUE TRUEstringwith..."
-- Iteration 2 --
2007-05-14 13:05:29 +00:00
string(36) "2101-639111PHP1111 1stringwith..."
-- Iteration 3 --
2007-05-14 13:05:29 +00:00
string(27) "20-6391PHP stringwith..."
-- Iteration 4 --
Notice: Array to string conversion in %s on line %d
string(13) "key1Arraykey2"
-- Iteration 5 --
2007-05-14 13:05:29 +00:00
string(27) "20-6391PHP stringwith..."
-- Iteration 6 --
2007-05-14 13:05:29 +00:00
string(36) "2 0 -639 1 PHP stringwith..."
-- Iteration 7 --
2007-05-14 13:05:29 +00:00
string(153) "2stringbetween0stringbetween-639stringbetween1stringbetweenPHPstringbetweenstringbetweenstringbetweenstringbetween stringbetweenstringwith..."
-- Iteration 8 --
2007-05-14 13:05:29 +00:00
string(27) "20-6391PHP stringwith..."
-- Iteration 9 --
2007-05-14 13:05:29 +00:00
string(36) "2000-639010PHP0000 0stringwith..."
-- Iteration 10 --
2007-05-14 13:05:29 +00:00
string(45) "2\00\0-639\01\0PHP\0\0\0\0 \0stringwith..."
*** Testing implode() on empty string ***
Warning: implode(): Argument must be an array in %s on line %d
NULL
*** Testing implode() on sub-arrays ***
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
string(27) "ArrayTESTArrayTESTPHPTEST50"
Notice: Array to string conversion in %s on line %d
string(19) "1Array2Array3Array4"
2006-08-22 19:51:34 +00:00
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
string(18) "Array2Array2PHP250"
*** Testing implode() on objects ***
string(13) "Object,Object"
2006-08-22 19:51:34 +00:00
array(2) {
[0]=>
&object(foo)#%d (0) {
2006-08-22 19:51:34 +00:00
}
[1]=>
&object(foo)#%d (0) {
2006-08-22 19:51:34 +00:00
}
}
*** Testing end() on resource type ***
2007-05-14 13:05:29 +00:00
string(%d) "Resource id #%d::Resource id #%d"
*** Testing error conditions ***
Warning: Wrong parameter count for implode() in %s on line %d
NULL
Warning: implode(): Argument must be an array in %s on line %d
NULL
Warning: implode(): Invalid arguments passed in %s on line %d
NULL
Warning: implode(): Invalid arguments passed in %s on line %d
NULL
string(0) ""
Warning: implode(): Invalid arguments passed in %s on line %d
NULL
Warning: implode(): Invalid arguments passed in %s on line %d
NULL
Warning: Wrong parameter count for implode() in %s on line %d
NULL
2006-08-22 19:51:34 +00:00
Done
2007-05-14 13:05:29 +00:00
--UEXPECTF--
*** Testing implode() for basic opeartions ***
unicode(4) "1, 2"
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
unicode(8) "1.1, 2.2"
array(2) {
[0]=>
float(1.1)
[1]=>
float(2.2)
}
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
unicode(12) "Array, Array"
array(2) {
[0]=>
array(1) {
[0]=>
int(2)
}
[1]=>
array(1) {
[0]=>
int(1)
}
}
unicode(3) ", 1"
array(2) {
[0]=>
bool(false)
[1]=>
bool(true)
}
unicode(0) ""
array(0) {
}
unicode(42) "a, aaaa, b, bbbb, c, ccccccccccccccccccccc"
array(6) {
[0]=>
unicode(1) "a"
[1]=>
unicode(4) "aaaa"
[2]=>
unicode(1) "b"
[3]=>
unicode(4) "bbbb"
[4]=>
unicode(1) "c"
[5]=>
unicode(21) "ccccccccccccccccccccc"
}
*** Testing implode() with variations of glue ***
-- Iteration 1 --
unicode(63) "2TRUE0TRUE-639TRUE1TRUEPHPTRUETRUETRUETRUE TRUEstringwith..."
-- Iteration 2 --
unicode(36) "2101-639111PHP1111 1stringwith..."
-- Iteration 3 --
unicode(27) "20-6391PHP stringwith..."
-- Iteration 4 --
Notice: Array to string conversion in %s on line %d
unicode(13) "key1Arraykey2"
-- Iteration 5 --
unicode(27) "20-6391PHP stringwith..."
-- Iteration 6 --
unicode(36) "2 0 -639 1 PHP stringwith..."
-- Iteration 7 --
unicode(153) "2stringbetween0stringbetween-639stringbetween1stringbetweenPHPstringbetweenstringbetweenstringbetweenstringbetween stringbetweenstringwith..."
-- Iteration 8 --
unicode(27) "20-6391PHP stringwith..."
-- Iteration 9 --
unicode(36) "2000-639010PHP0000 0stringwith..."
-- Iteration 10 --
unicode(45) "2\00\0-639\01\0PHP\0\0\0\0 \0stringwith..."
*** Testing implode() on empty string ***
2008-02-24 02:22:15 +00:00
Warning: implode(): Argument must be an array in %s on line %d
NULL
2007-05-14 13:05:29 +00:00
*** Testing implode() on sub-arrays ***
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
unicode(27) "ArrayTESTArrayTESTPHPTEST50"
Notice: Array to string conversion in %s on line %d
unicode(19) "1Array2Array3Array4"
Notice: Array to string conversion in %s on line %d
Notice: Array to string conversion in %s on line %d
unicode(18) "Array2Array2PHP250"
*** Testing implode() on objects ***
unicode(13) "Object,Object"
array(2) {
[0]=>
&object(foo)#%d (0) {
}
[1]=>
&object(foo)#%d (0) {
}
}
*** Testing end() on resource type ***
unicode(%d) "Resource id #%d::Resource id #%d"
*** Testing error conditions ***
Warning: Wrong parameter count for implode() in %s on line %d
NULL
Warning: implode(): Argument must be an array in %s on line %d
NULL
2007-05-14 13:05:29 +00:00
Warning: implode(): Invalid arguments passed in %s on line %d
NULL
2007-05-14 13:05:29 +00:00
Warning: implode(): Invalid arguments passed in %s on line %d
NULL
2007-05-14 13:05:29 +00:00
unicode(0) ""
Warning: implode(): Invalid arguments passed in %s on line %d
NULL
2007-05-14 13:05:29 +00:00
Warning: implode(): Invalid arguments passed in %s on line %d
NULL
2007-05-14 13:05:29 +00:00
Warning: Wrong parameter count for implode() in %s on line %d
NULL
Done