[Solved] Difference between explode and implode functions in php?

  

3
Topic starter

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

Can you give examples of using the both?

Thanks

1 Answer
2

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;

Share: