Explain PHP explode() function

The PHP explode() function breaks a string into an array.

The explode() function in PHP is used to split a string into an array of substrings based on a specified delimiter. Here’s a breakdown of its syntax and functionality:

Syntax:

php
array explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] )

Parameters:

  • $delimiter: This parameter specifies the character or characters upon which the string will be split. It can be a single character or a string containing multiple characters.
  • $string: This parameter represents the input string that will be split.
  • $limit (optional): This parameter specifies the maximum number of elements in the resulting array. If provided, the resulting array will contain a maximum of $limit elements, with the last element containing the remainder of the input string.

Return Value:

  • The function returns an array of strings obtained by splitting the input string $string using the delimiter $delimiter.

Example:

php
$string = "apple,banana,orange,grape";
$array = explode(",", $string);
print_r($array);

Output:

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

In this example, the explode() function splits the string $string using the delimiter ",". As a result, it creates an array where each element corresponds to a substring between the delimiters.