settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Difference between explode and implode functions in php?

+2 votes
In Php what is the difference between implode and explode function?

Can you give examples of using the both?

Thanks
asked in PHP category by user ak47seo
edited by user golearnweb

1 Answer

+2 votes

Explode and implode functions in php are pretty popular; 

Explode splits string by delimiter;

Here is an example with an array:

<?php

//Function explode() example
$str  = 'Silvester Stalone Rambo';
$data = explode( ' ', $str );

echo '<pre>';
print_r( $data );
echo '</pre>';

foreach ( $data as $v ) {
	echo "$v" . '<br>';
}

Result:

explode function php example

Implode join array elements with a string (the opposite of explode);

Example:

<?php

//Function implode() example
$data2 = [ 'Silvester', 'Stalone', 'Rambo' ];

$rambo = implode( ' ', $data2 );
echo '<pre>';
echo $rambo;
echo '</pre>';

Result:

implode function php example

You can also use the join function instead of implode (join is an alias of: implode function).

Join function is rarely used, in PHP mostly implode is used in collaboration with explode;

answered by user eiorgert
edited by user eiorgert
...