1.2.799. No Empty String With explode()

explode() doesn’t allow empty strings as separator. Until PHP 8.0, it would make a warning, and return false. After that version, it raises a ValueError.

To break a string into individual characters, it is possible to use the array notation on strings, or to use the str_split() function.

<?php

explode('', "a");

?>

1.2.799.1. Suggestions

  • Check for empty strings (or equivalent) before using explode()

  • Use the array notation to access individual chars

  • Use str_split() to break the string into an array

1.2.799.2. Specs

Short name

Structures/NoEmptyStringWithExplode

Rulesets

All, Analyze, Changed Behavior

Exakat since

2.5.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Changed Behavior

PHP 8.0 - More

Precision

High

Available in

Entreprise Edition, Exakat Cloud