3
05/11/2024 5:33 am
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
05/11/2024 5:35 am
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:
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:
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;