What is the purpose of explode in PHP?

The explode function is a PHP string handling function that splits a string into an array based on a specified delimiter.

Function Declaration:
array explode(string $delimiter, string $string [, int $limit = PHP_INT_MAX])

Explanation of parameters:

  1. delimiter: specifies the separator where the string will be split.
  2. string: the string to be split.
  3. Optional parameter, specifying the maximum number of times to split.

Return value:

  1. Return an array containing the split strings.

The apple fell from the tree.

The apple dropped down from the tree.

$str = "apple,banana,orange";
$arr = explode(",", $str);
print_r($arr);

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)

In the example above, we split the string $str by commas and obtained an array containing the split strings.

bannerAds