PHP implode() Function

PHP implode() function is used to join the elements of an array into a string. implode() is an alias for the PHP join() function.

Syntax

implode(separator,$array)

Parameters

  1. separator(optional): It specifies what to put between the array elements. The default is an empty string(“”).
  2. $array(required):  It is the array to join the string.

Return value

It returns the joined string formed from the elements of the array.

If you pass an empty array to implode(), it returns an empty string.

Visual Representation

Visual Representation of PHP implode() Function

Example 1: How to Use implode () Function

<?php

$frontend = ['Svelete','JavaScript','Angular','React', 'Vue'];

echo implode($frontend);

Output

SveleteJavaScriptAngularReactVue

This method accepts its parameters in either order. However, for consistency with the explode() function, you should use the documented order of arguments.

Example 2: Using separator parameterVisual Representation of Using separator parameter

<?php

$frontend = ['Svelete','JavaScript','Angular','React','Vue'];

echo implode(" ",$frontend)."\n";
echo implode(" _ ",$frontend)."\n";
echo implode(" + ",$frontend)."\n";
echo implode(" | ",$frontend)."\n";

Output

Svelete JavaScript Angular React Vue
Svelete _ JavaScript _ Angular _ React _ Vue
Svelete + JavaScript + Angular + React + Vue
Svelete | JavaScript | Angular | React | Vue

Example 3: Associative array

The function acts on array “values,” disregarding any keys.

<?php

$devs = ['1st' => 'CloudArchitecht',
 '2nd' => 'DevOps',
 '3rd' => 'DataScientists'];

echo implode(" | ", $devs)."\n";

Output

CloudArchitecht | DevOps | DataScientists

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.