What is the difference between implode and explode function in php?
In coding many times we need to convert a string into array and array into string for that we used explode and implode in php.
The implode function is used to “join elements of an array with a string”.
The explode function is used to “Split a string by a specified string into pieces i.e. it breaks a string into an array”.
Implode() Function
The implode() function returns a string from the elements of an array.
Syntax:
string implode(string $seperator, array $array)
Example 1:
<?php
$arr = array('Nikunj','Joshi','PHP','Developer');
$str = implode(" ",$arr);
echo $str;
?>
Example 2:
<?php
$arr = array('Nikunj','Joshi','PHP','Developer');
$str = implode("-",$arr);
echo $str;
?>
Explode() Function
The explode() function breaks a string into an array.
Syntax:
array explode(string separator,string string)
<?php
$str = "Nikunj Joshi PHP Developer";
$arr = explode(" ",$str);
print_r($arr);
?>
Example 2:<?php
$str = "Nikunj-Joshi-PHP-Developer";
$arr = implode("-",$str);
print_r($arr);
?>
If you have any query or suggestions, feel free to ask me via the comment section below.
must watch some other topics :
0 Comments