What is implode() function in PHP
The implode() function is a binary-safe function in PHP, which used to join elements of an array. It merges all the elements of an array and returns a string.
Syntax
implode(separator, array)
Parameter
The implode() function takes two parameters one optional and one compulsory.
Parameter | Type | Description |
separator (optional) | string type |
This parameter separates the elements of an array joined to form a string. Default value: empty string |
array (mandatory) | array | This parameter specifies the array whose elements are to be imploded (joined) |
Notes:
- PHP implode() accepts its parameter in either order.
- The separator parameter in implode() function became optional in PHP version 4.3.0, but it is recommended to use atleast two-parameter for backward compatibility.
Return Value:
This function returns a string containing all the elements of input array in the same order, along with separator(if specified).
Example of implode() function in PHP
<?php
//PHP Program to illustrate the working of implode() function
//Array Variable
$cars = array('Maruti','Hyundai','Honda','Toyota','JEEP');
//Variable to store the data after implode
$implodeCars = implode(", ", $cars);
//Printing the output
print_r($implodeCars);
?>
Output
Maruti, Hyundai, Honda, Toyota, JEEP