PHP implode() Function
The implode()
function returns a string consisting of the elements of the array. This function accepts parameters in any order. However, for consistency with explosion(), you must use the documented argument order. The separator parameter for implode() is optional. However, for backward compatibility, we recommend that you always use two parameters. This function is binary safe.
Syntax
implode(separator,array)
- separator - (Optional) Specifies what to insert between array elements. The default value is "" (empty string).
- array - (Required) Array to concatenate with string.
Example
<?php $str = array('Hi','Hello','Welcome','World'); echo implode(" ",$str); ?>
Output
Hi Hello Welcome World
Concatenate array elements with string
Example
<?php $str = array('Hi','Hello','Welcome','World'); //implode(" ", $str) will join the elements with a Space as the separator. echo implode(" ", $str)."<br>"; //implode("+", $str) will join the elements with a Plus sign as the separator. echo implode("+", $str)."<br>"; //implode("-", $str) will join the elements with a Hyphen as the separator. echo implode("-", $str)."<br>"; //implode("*", $str) will join the elements with the letter '*' as the separator. echo implode("*", $str); ?>
Output
Hi Hello Welcome World Hi+Hello+Welcome+World Hi-Hello-Welcome-World Hi*Hello*Welcome*World