|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-07-19 20:24 UTC] matthew at binaryrefined dot net
Description:
------------
When providing an empty source string to explode, it returns an array with a
single, empty element. Expected behavior was an empty array.
Test script:
---------------
<?php
$x = explode('|', ''); // '' would be a variable, and not intentionally empty.
print_r($x);
?>
Expected result:
----------------
Array ()
Actual result:
--------------
Array ( [0] => )
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 16 13:00:01 2025 UTC |
To block against this, I've had to do a boolean check against the variable before applying explode to ensure an empty source string will produce an empty array: if ($x) { $x = explode('|', $x); } else { ... } // $x is empty, and if passed to explode, results in an array with a single, empty element. I've been developing with PHP for ~10 years and I cannot ever remember having to do this with explode. The more I think about it, the more I believe this may be a bug? Thanks for the time.