Concept of the implode()
Function
The implode()
function joins array elements into a string using a custom delimiter.
This function is especially useful for formatting array values into a simple string for output or storage.
$colors = ['red', 'green', 'blue'];
$comma_separated = implode(', ', $colors); // Join with a comma delimiter
echo $comma_separated; // Output: 'red, green, blue'
In this example, the elements of the array $colors
are joined together into a single string, separated by a comma and a space ', '
. The resulting string is 'red, green, blue'
.
Note:
This function is also known as join()
. Both names work the same way.
Syntax
implode(string $separator, array $array): string
Parameters
$separator |
Optional. The delimiter used to join the elements of the array.
Defaults to an empty string ( '' ). |
---|---|
$array |
The implode() function returns a string containing the array elements joined by the specified separator, in the same order as the original array. |
Return Values
implode()
함수는 배열의 요소들을 순서대로 구분자로 합쳐서 생성한 문자열을 반환합니다.
Changelog
Version | Description |
---|---|
8.0.0 | Using the separator as the second argument (after the array) is no longer supported. |
7.4.0 | Using the legacy argument order—placing the separator after the array—has been deprecated. |
The implode()
function is intended to be used in the following form:
implode(string $separator, array $array);
However, in older versions of PHP, it was also possible to use the reverse argument order:
implode(array $array, string $separator); // Legacy syntax (deprecated or removed)
Caution
Be aware that the array passed to implode()
may contain boolean values.
In such cases, the function can produce unexpected results due to type conversion.
$booleans_array = [true, true, false, false, true];
$result = implode('', $booleans_array);
var_dump($result); // Output: string(3) "111"
// true is converted to "1", and false becomes an empty string.
This behavior occurs because implode()
automatically converts non-string values to strings. Booleans in particular can lead to subtle bugs if not handled carefully.
Practical Examples
The following examples demonstrate how the implode()
function behaves in various scenarios.
Basic Usage
$fruits = ['apple', 'banana', 'cherry'];
$fruits_string = implode(', ', $fruits);
echo $fruits_string; // Output: 'apple, banana, cherry'
In this example, the elements of the $fruits
array are joined into a single string using a comma and a space (', '
) as the delimiter.
Generating an HTML List
$items = ['Item 1', 'Item 2', 'Item 3'];
$html_list = '<ul><li>' . implode('</li><li>', $items) . '</li></ul>';
echo "HTML List:\n" . $html_list;
- Item 1
- Item 2
- Item 3
This example joins the array elements into an HTML unordered list by wrapping each item with <li>
tags and enclosing the whole result in <ul>
.
Imploding an Array of Objects
class StringArray {
protected $title;
public function __construct($title)
{
$this->title = $title;
}
public function __toString()
{
return $this->title;
}
}
$array = [
new StringArray('Spring'),
new StringArray('Summer'),
new StringArray('Autumn'),
new StringArray('Winter')
];
echo implode('; ', $array); // Output: 'Spring; Summer; Autumn; Winter'
If the array contains objects, implode()
will call the __toString()
method on each object. In this example, each season name is returned as a string and joined with a semicolon and space ('; '
).
Empty Array
$empty = [];
$result = implode(', ', $empty);
var_dump($result); // Output: string(0) ""
If the array is empty, implode()
returns an empty string instead of throwing an error. This behavior is safe but may result in unintended blank output if not checked properly.
Building a SQL Query
$ids = [10, 20, 30];
$query = "SELECT * FROM users WHERE id IN (" . implode(', ', $ids) . ")";
echo $query;
// Output: SELECT * FROM users WHERE id IN (10, 20, 30)
This example shows a common real-world use case: dynamically building a SQL query using array values. implode()
helps format the list of IDs into a comma-separated string. Make sure to validate or sanitize values when working with user input to prevent SQL injection.