implode() joins array values using a separator. Use the current argument order: separator first, then the array.
$colors = ['red', 'green', 'blue'];
echo implode(', ', $colors); // red, green, blue
Start with this current pattern. The examples below are retained, corrected where necessary, and expanded for additional variations and output.
$ar=array('Welcome','to','plus2net','php','section');
echo implode($ar);
The output is here
Welcometoplus2netphpsection
Sorry , we missed the space between words so let us add one gap between
$ar=array('Welcome','to','plus2net','php','section');
echo implode(" ",$ar);
The output is here
Welcome to plus2net php section
<?php
$ar = array('apple', 'banana', 'cherry');
echo implode(" - ", $ar); // Output: apple - banana - cherry
?>
<?php
$ar = array('P', 'H', 'P');
echo implode("", $ar); // Output: PHP
?>
<?php
$ar = array('name' => 'John', 'age' => 25);
echo implode(", ", $ar); // Output: John, 25
?>
<?php
$multi_array = array(array('a', 'b'), array('c', 'd'));
foreach($multi_array as $arr) {
echo implode(", ", $arr) . "<br>"; // Output: a, b and c, d on new lines
}
?>
<?php
$empty_array = array();
echo implode(", ", $empty_array); // Output: (empty string)
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.