php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42129 restrict explode's function return type
Submitted: 2007-07-27 22:01 UTC Modified: 2014-12-31 16:24 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: david dot nqd at gmail dot com Assigned:
Status: Open Package: *General Issues
PHP Version: 5.2.3 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
41 - 22 = ?
Subscribe to this entry?

 
 [2007-07-27 22:01 UTC] david dot nqd at gmail dot com
Description:
------------
This is not a bug report like #38337.

Shouldn't the return type of explode be restricted to an array given valid inputs (strings)?
This would mean that if an empty string was passed as the delimiter, the return would be an array containing individual characters.

I doubt this would break any existing code unless people used explode to check that the delimiter === ''

Reproduce code:
---------------
<?php
$x = explode('', 'abc');
?>

Expected result:
----------------
$x = array('a','b','c')

Actual result:
--------------
$x = false

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-19 14:52 UTC] alastair at propcom dot co dot uk
This affects me too.

I don't want this to be my string-split paradigm:

if ($delimiter === '') return str_split($str);
else return explode($delimiter, $str);

That's just stupid. Splitting on the empty string is a completely logical and 
well-defined operation.
 [2014-12-31 16:24 UTC] nikic@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2014-12-31 16:24 UTC] nikic@php.net
Note that if explode('', 'abc') were implemented the correct result would actually be array('', 'a', 'b', 'c', ''), which would be unexpected to many people. To avoid confusion PHP does not allow this usage.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Oct 19 20:01:26 2019 UTC