Skip to content
PHP Logo

How can the explode() and implode() functions be effectively used together to achieve the desired result in PHP?

To achieve the desired result using explode() and implode() functions together in PHP, you can first use explode() to split a string into an array based on a delimiter, manipulate the array elements as needed, and then use implode() to join the modified array elements back into a string. Example:

php
$string = "apple,banana,cherry";
$array = explode(",", $string);

// Manipulate array elements
foreach ($array as &$value) {
    $value = strtoupper($value);
}

$newString = implode("-", $array);
echo $newString; // Output: APPLE-BANANA-CHERRY