Doc #62613 [Opn->Nab]: 'explode' quirk
| From: | bjori@php.net | Date: | Thu, 19 Jul 2012 21:19:58 +0000 |
| Subject: | Doc #62613 [Opn->Nab]: 'explode' quirk | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8585@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62613&edit=1
ID: 62613
Updated by: bjori@php.net
Reported by: matthew at binaryrefined dot net
Summary: 'explode' quirk
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: Arrays related
Operating System: Win XP, SP3
PHP Version: 5.4.4
Block user comment: N
Private report: N
New Comment:
Why would it be empty?
explode() creates an new array, finds the first element of $delimiter chops it
away and pushes the element to the array.
Then continues until there are no more instances of $delimiter, then pushes the
last string to the return array...
Which means.. If there are no hits of the $delimiter, you'll just get the same
string.. in an array of one element.. the same original string.
Why should empty argument have an special treatment? o.O
Previous Comments:
------------------------------------------------------------------------
[2012-07-19 20:49:25] matthew at binaryrefined dot net
To block against this, I've had to do a boolean check against the variable before applying
explode to ensure an empty source
string will produce an empty array:
if ($x)
{
$x = explode('|', $x);
}
else
{ ... } // $x is empty, and if passed to explode, results in an array with a single, empty
element.
I've been developing with PHP for ~10 years and I cannot ever remember having to do this with
explode. The more I think about
it, the more I believe this may be a bug?
Thanks for the time.
------------------------------------------------------------------------
[2012-07-19 20:24:18] matthew at binaryrefined dot net
Description:
------------
When providing an empty source string to explode, it returns an array with a
single, empty element. Expected behavior was an empty array.
Test script:
---------------
<?php
$x = explode('|', ''); // '' would be a variable, and not
intentionally empty.
print_r($x);
?>
Expected result:
----------------
Array ()
Actual result:
--------------
Array ( [0] => )
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62613&edit=1