php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31443 explode() returns weird array on string that starts with delimiter
Submitted: 2005-01-07 20:11 UTC Modified: 2005-01-07 21:08 UTC
From: walter at unpleasant dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.10 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: walter at unpleasant dot net
New email:
PHP Version: OS:

 

 [2005-01-07 20:11 UTC] walter at unpleasant dot net
Description:
------------
When explode() explodes a string that starts with the delimiter, it returns an array that confuses reset(). The array is not empty, but reset()ing it returns FALSE.

The same thing happens with split().

This messes up when you try to iterate over the result like this:

for($i = reset($array);$i;$i = next($array) { ... }



Reproduce code:
---------------
$array = split(',', ',a,b,c');
echo count($array) . "Count is \n";
if(reset($array) == FALSE) echo "Can't happen.\n";


Expected result:
----------------
If I understand the semantics of reset() correctly, it should only returns FALSE on empty arrays. The array above is clearly not empty (count($array) returns 4), so resetting it should not return FALSE.


Actual result:
--------------
The above script prints out this when run:

Count is 4
Can't happen.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-07 21:05 UTC] sniper@php.net
You should try 'var_dump()' sometimes..
From manual:

"reset() rewinds array's internal pointer to the first element and returns the value of the first array element"

First element in the array you get from the string which starts with the delimeter is...tada..EMPTY!

"" == FALSE but FALSE !== ""

In other words: use === to compare reliably..

NO bug here.

 [2005-01-07 21:08 UTC] walter at unpleasant dot net
Pardon me, it seems my brain had an out of coffee error.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 29 11:00:03 2025 UTC