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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 20:01:34 2024 UTC