In PHP 8.5, the explode() function remains unchanged in its core behavior. It is used to split a string into an array of substrings based on a specified separator .
Basic Example
Here is how you can split a sentence into an array of words:
<?php
$text = "Hello World from PHP";
$words = explode(" ", $text);
print_r($words);
?>
Output:
Array
(
[0] => Hello
[1] => World
[2] => from
[3] => PHP
)
Important PHP 8.5 Behavior
While the function is stable, you must remember that if you provide an empty string as the separator, explode() will throw a ValueError. This behavior has been in place since PHP 8.0 and continues in 8.5 .
// This will cause a ValueError in PHP 8.5
// $array = explode("", "test");
Using the limit Parameter
You can control the number of elements returned using the third parameter:
<?php
$data = "one,two,three,four";// Positive limit: returns max 2 elements, last element has the rest
print_r(explode(",", $data, 2));
// Output: [0] => one, [1] => two,three,four// Negative limit: removes the last element
print_r(explode(",", $data, -1));
// Output: [0] => one, [1] => two, [2] => three
?>