php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37048 str_split returns non-empty array when given empty string as argument
Submitted: 2006-04-12 02:37 UTC Modified: 2006-04-12 14:50 UTC
From: gtg782a at mail dot gatech dot edu Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.1.2 OS: RH 7.3
Private report: No CVE-ID:
 [2006-04-12 02:37 UTC] gtg782a at mail dot gatech dot edu
Description:
------------
In the lastest version of PHP (5.1.2), running str_split with an empty string returns an array containing a single element, which is also an empty string. This seems counterintuitive and is a different result than in previous versions of PHP5. Rather than return a single-element array with an empty string, I would expect an empty array with no elements. (5.0.4 evaluates in the latter way - giving the more intuitive result).

This makes a difference when the resulting array is used as the argument to foreach, for example, such as foreach(str_split($string) as $letter). If a string is empty, it would be intuitive that foreach would never execute its inner code.

If this is an intended behavior, then the version difference ought to be noted in the documentation. I do think, however, that the old behavior made more sense.

Reproduce code:
---------------
var_dump(str_split(""));

Expected result:
----------------
array(0) { }

Actual result:
--------------
array(1) { [0]=>  string(0) "" }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-12 14:50 UTC] sniper@php.net
RTFM "If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element."
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Thu Aug 17 11:01:43 2017 UTC