String Functions

PHP implode() Function

Topic: PHP String ReferencePrev|Next

Description

The implode() function join array elements into a single string.

The following table summarizes the technical details of this function.

Return Value: Returns a string created by joining array's elements.
Changelog: Passing the separator after the array has been deprecated since PHP 7.4.0
Version: PHP 4+

Syntax

The basic syntax of the implode() function is given with:

implode(separator, array);

The following example shows the implode() function in action.

<?php
// Sample array
$array = array("one", "two", "three", "four", "five");

// Creating comma separated string from array elements
echo implode(",", $array);
?>

Parameters

The implode() function accepts the following parameters.

Parameter Description
separator Optional. Specifies the string to be inserted between each element of the array while joining. It is also called glue string. Default is an empty string ("").
array Required. Specifies the array to be imploded.

More Examples

Here're some more examples showing how implode() function actually works:

The following example shows how to join array elements using different characters.

<?php
// Sample array
$array = array("one", "two", "three", "four", "five");

// Imploding array with different separators
echo implode(", ", $array); // comma plus space
echo implode("_", $array);  // underscore
echo implode("-", $array);  // hyphen
echo implode(" ", $array);  // space
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties