😄 This post is based on the content from coding.courses.
Definition and Usage
(PHP Version | 4+)
The implode() function joins array elements into a string using a custom delimiter.
This function returns a single string by joining each element of a given array with a specified delimiter.
Basic Example
$colors = ['red', 'green', 'blue']; $comma_separated = implode(', ', $colors); // Join with a ', ' 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'.
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 array to join into a string.
Return Values
The implode() returns a string created by joining the elements of an array in order, separated by a specified delimiter.
Changelog
# 8.0.0 Version
Using the separator as the second argument (after the array) is no longer supported.
# 7.4.0 Version
Using the legacy argument order—placing the separator after the array—has been deprecated.
Practical Examples
# 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; 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>.
# Objects Can Be Included in the Array Passed to the implode() Function
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.


