explode() Forbids Empty Strings¶
explode() doesn’t work on empty strings, as delimiter (first argument). It used to be a warning and a returned value of false, it is now a Fatal error.
PHP code¶
<?php
explode('', 'abc');
?>
Before¶
PHP Warning: explode(): Empty delimiter in /codes/explodeWithEmptyString.php on line 3
Warning: explode(): Empty delimiter in /codes/explodeWithEmptyString.php on line 3
After¶
PHP Fatal error: Uncaught ValueError: explode(): Argument #1 ($separator) cannot be empty in /codes/explodeWithEmptyString.php:3
Stack trace:
#0 /codes/explodeWithEmptyString.php(3): explode('', 'abc')
#1 {main}
thrown in /codes/explodeWithEmptyString.php on line 3
Fatal error: Uncaught ValueError: explode(): Argument #1 ($separator) cannot be empty in /codes/explodeWithEmptyString.php:3
Stack trace:
#0 /codes/explodeWithEmptyString.php(3): explode('', 'abc')
#1 {main}
thrown in /codes/explodeWithEmptyString.php on line 3
PHP version change¶
This behavior changed in 8.0