php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22226 explode misbehaves exploding empty strings
Submitted: 2003-02-14 16:47 UTC Modified: 2003-02-15 13:28 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: php at savignano dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.1 OS: Win32
Private report: No CVE-ID: None
 [2003-02-14 16:47 UTC] php at savignano dot de
This was reported before (bug #8883) as 

count(explode("|", "")) == 1

but was stated bogus for some unexplained reason.

The problem is: exploding an empty string returns an array with one empty element, but should return an empty array instead. 

I found this bug to be closed for PHP 3.0.3 (bug #699) with no comment, but it seems it isn't fixed really?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-15 11:04 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

If the separator string cannot be found the entire contents of the passed string will be stored in the 1st element of the returned array. This is intended behaviour.
 [2003-02-15 13:15 UTC] php at savignano dot de
Thanks for the reply. I understand that it is intended that, if the separator string cannot be found, the entire contents of the string will be stored in the 1st element of the returned array. This makes perfect sense, if there IS a contents. But if there is no contents because the string is empty, this is not the behaviour one would expect.

Now why do I expect a different behaviour? Let's use a http get parm as an obvious example: 

explode("&", "a=b&x=y") returns array("a=b", "x=y")
I expect this of course.

explode("&", "a=b")     returns array("a=b")
Same here. I have a get parm, I get an array entry.

explode("&", "")        returns array("")
But here? I have nothing but nonetheless an array entry is returned? This means I have to pre-check the string for the special case of an empty string since usually this means "no data", "no parm", "no entry".

BTW, explode returns a one element array even if it explodes NULL instead of an empty string. Now that is strange, isn't it?

I re-opened the bug but won't bother you any more if you insist to put it back to bogus once more. Thanks fo your time.
 [2003-02-15 13:28 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

"" is not NULL, which in this case would work the same anyway because the function expects you to pass 2 strings, the separator & the string to break. Therefor any values passed to the function will 1st be converted to strings meaning that you can never get anything but an array with atleast 1 element as the return value.
If you want to validate the string manually use the empty() function before calling explode(). Altering the behaviour of this function would result in breaking of backwards compatibility and is not an option.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Mon Jan 12 01:00:01 2026 UTC