From: David R. <da...@ro...> - 2009-02-20 23:50:01
|
I think it's returning what I would expect it to return. Because PHP doesn't really differential between an empty string and a null string, then a string is a string. Looking at the php manual for explode (http://php.net/explode): Return Values - If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string , then explode() will return an array containing string. So, in your case, it's returning $string back to you as the first element of the array... exactly as expected. Now, if $string was a null, uninitialized variable, perhaps you have a point, but I think it's pretty much working as intended. Perhaps a string-length check is appropriate in this circumstance before you explode the string? Or, when you do a count($array) in a conditional, also do a len($string)? -Rovani (please don't take it as condescending... definitely not meant that way) On Fri, Feb 20, 2009 at 3:00 PM, Arlo Leach <ar...@ar...> wrote: > Hi folks, > > Consider the following PHP code, which explodes an empty string: > > $string = ""; > $array = explode("|", $string); > print_r($array); > print "Length: ".count($array); > > I would expect this to output an empty array: > > Array ( ) Length: 0 > > But it actually outputs: > > Array ( [0] => ) Length: 1 > > Does anyone else find this odd? It has bitten me a few times lately, when I > test the existence of data with a count(), or try to process data with a FOR > loop. I end up writing this a lot to get the expected results: > > $array = ($string) ? explode(",", $string) : array() ; > > I was considering logging a bug report but wondered what your thoughts were. > > Cheers, > -Arlo > > _______________________________ > > Arlo Leach > 773.769.6106 > http://arlomedia.com > > > > > ------------------------------------------------------------------------------ > Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA > -OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise > -Strategies to boost innovation and cut costs with open source participation > -Receive a $600 discount off the registration fee with the source code: SFAD > http://p.sf.net/sfu/XcvMzF8H > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss > |