What is the usage of explode in PHP?
The explode function is used to split a string into an array. Its usage is as follows:
Split the string using the specified delimiter and optional limit.
Explanation of parameters:
- Delimiter: specifies the separator for the given string, which can be a single character or a string.
- String: The string to be split.
- Limit (optional): Specifies the maximum number of elements that can be included in the returned array.
Return value: returns an array containing the substrings obtained by splitting the string based on the specified delimiter.
I am currently studying for my exams.
I am currently preparing for my exams.
$str = "Hello,World,PHP";
$arr = explode(",", $str);
print_r($arr);
Output:
Array
(
[0] => Hello
[1] => World
[2] => PHP
)
In the given example, the string “$str” is split by commas to create an array “$arr” with the elements being the substrings after the split.