PHP implode() Function
The implode() function in PHP joins the specified array elements with a string.
Syntax
1 2 3 |
implode ( string $glue, array $pieces ) |
Parameter
glue(optional)- It specifies what to put between the array elements.
pieces(required)- This parameter represents the array of strings to implode.
Return
This function returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.
Example 1
1 2 3 4 5 6 7 8 9 10 |
<?php // initializing the string with the array echo "Before using the implode() function: array('Hello','TutorialandExample')"."\n"; //passing the array $arr = array('Hello','TutorialandExample'); // joining the specified array elements with a string echo "After using 'implode()' function: ".implode(" ",$arr);; ?> |
Output
1 2 3 4 |
Before using implode() function: array('Hello','TutorialandExample') After using 'implode()' function: Hello TutorialandExample |
Example 2
1 2 3 4 5 6 7 8 9 10 |
<?php // initializing the string with the array echo "Before using the implode() function: array('Hello','TutorialandExample')"."\n"; //passing the array $arr = array('Hello','Tutorial','and','Example'); // joining the specified array elements with a string echo "After using 'implode()' function: ".implode("+",$arr);; // putting th + between the array elements. ?> |
Output
1 2 3 4 |
Before using the implode() function: array('Hello','TutorialandExample') After using 'implode()' function: Hello+Tutorial+and+Example |
Example 3
1 2 3 4 5 6 7 8 9 10 |
<?php // initializing the string with the array echo "Before using the implode() function: array('Hello','TutorialandExample')"."\n"; //passing the array $arr = array('Hello','Tutorial','and','Example'); // joining the specified array elements with a string echo "After using 'implode()' function: ".implode("*",$arr);; // putting th * between the array elements. ?> |
Output
1 2 3 4 |
Before using the implode() function: array('Hello','TutorialandExample') After using 'implode()' function: Hello*Tutorial*and*Example |