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
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 !
Your email address:
MUST BE VALID
Solve the problem:
42 - 18 = ?
Subscribe to this entry?

 
 [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: Sat Jun 01 05:01:29 2024 UTC