What is the difference between explode() and implode() function?

222 Views

What is the difference between explode() and implode() function?

 PHP | implode() and explode() function

implode() function is use to convert an array into a string where as explode() function is used to convert an string into an array

 

implode()

The implode() function returns a string from elements of an array. It takes an array of strings and joins them together into one string using any delimiter.

 <?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>

// output will be: Hello World! Beautiful Day!

explode()

The explode() function in PHP allows us to splits a string into array by string. Using the explode() function we will create an array from a string.

 <?php

$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));

?>

// output will be: 

Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] => day. )

The explode() function breaks a string into an array, but the implode function returns a string from the elements of an array.

Tags:-
PHP