From: Arlo L. <ar...@ar...> - 2009-02-20 21:28:52
|
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 |
From: Michael L. <mla...@ca...> - 2009-02-20 23:15:41
|
Arlo, I've run into that and though it's annoying, I think it's part of the "desired behavior" for the function. >From php.net/explode "If delimiter contains a value that is not contained in string , then explode() will return an array containing string" So, since your string doesn't contain a "|" is returns the "array" of an empty string. Make sense? -----Original Message----- From: Arlo Leach [mailto:ar...@ar...] Sent: Friday, February 20, 2009 3:00 PM To: Chicago PHP User Group Subject: [chiPHPug-discuss] troublesome PHP behavior 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 |
From: Arlo L. <ar...@ar...> - 2009-02-20 23:28:37
|
> "If delimiter contains a value that is not contained in string , then > explode() will return an array containing string" > > So, since your string doesn't contain a "|" is returns the "array" of an > empty string. Make sense? That does make sense -- so PHP is functioning as documented. But does this seem like the right behavior to others? I think it would be the expected behavior if $string is not empty, but if $string is empty, I would expect $array to be empty, too, regardless of the delimiter. Or, if this is a hopeless cause, does anyone have a better workaround than my little conditional check? Thanks! -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com |
From: Trevor O. <tr...@gm...> - 2009-02-20 23:47:03
|
I'd probably use preg_match_all, or your method, depending on what sort of data i was parsing. Here's some funny behavior I saw yesterday, but is also as intended. One expects x%10 to always return a number between 0 and 9 inclusive. However, -16%10 is -6. The answer makes sense (-16/10 = -1r-6) but I kind of expected it to just add another 10 onto it, so it became 4. For my expected behavior, I had to say ((x%y)+y)%y 2009/2/20 Arlo Leach <ar...@ar...> > > "If delimiter contains a value that is not contained in string , then > > explode() will return an array containing string" > > > > So, since your string doesn't contain a "|" is returns the "array" of an > > empty string. Make sense? > > That does make sense -- so PHP is functioning as documented. But does this > seem like the right behavior to others? I think it would be the expected > behavior if $string is not empty, but if $string is empty, I would expect > $array to be empty, too, regardless of the delimiter. > > Or, if this is a hopeless cause, does anyone have a better workaround than > my little conditional check? > > Thanks! > > -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 > -- -Trevor Oldak |
From: Jason R. <ja...@ho...> - 2009-03-17 14:34:36
|
Hey all, I believe we're supposed to have a meeting tomorrow? Has anyone sorted the logistics? I haven't confirmed with Sittercity or OCA regarding hosting it but it shouldn't be a problem. How many people are planning on attending? Anyone besides, Joe and I giving talks? -jason |
From: Jough D. <jou...@gm...> - 2009-03-17 14:42:27
|
I'll be attending. When and where? -- Jough On Tue, Mar 17, 2009 at 9:32 AM, Jason Rexilius <ja...@ho...>wrote: > Hey all, > > I believe we're supposed to have a meeting tomorrow? Has anyone > sorted the logistics? > > I haven't confirmed with Sittercity or OCA regarding hosting it but > it shouldn't be a problem. > > How many people are planning on attending? > > Anyone besides, Joe and I giving talks? > > -jason > > > ------------------------------------------------------------------------------ > Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are > powering Web 2.0 with engaging, cross-platform capabilities. Quickly and > easily build your RIAs with Flex Builder, the Eclipse(TM)based development > software that enables intelligent coding and step-through debugging. > Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss > -- Jough Dempsey http://jough.com 312.576.6738 (mobile) |
From: Kenneth D. <ke...@se...> - 2009-02-23 12:21:27
|
Arlo Leach wrote: >> "If delimiter contains a value that is not contained in string , then >> explode() will return an array containing string" >> >> So, since your string doesn't contain a "|" is returns the "array" of an >> empty string. Make sense? >> > > That does make sense -- so PHP is functioning as documented. But does this > seem like the right behavior to others? I think it would be the expected > behavior if $string is not empty, but if $string is empty, I would expect > $array to be empty, too, regardless of the delimiter. > What actually matters is what you do with it afterward. If we assume you are iterating the array, will you get undesirable affects from looping over a single empty value? If so, why? Let me ask this: if you had a string like this: "a|b||c", then explode would give 4 values and one of them would be empty. If you need a special handler for that then you've probably coded it already and your code should handle PHP's behavior just fine. If you do not need a special handler then your code will probably still run just fine. Is there some reason your downstream code will break on an array of one empty string? > Or, if this is a hopeless cause, does anyone have a better workaround than > my little conditional check? > > Thanks! > > -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 > -- Kenneth Downs Secure Data Software ke...@se... www.andromeda-project.org www.secdat.com Office: 631-689-7200 Cell: 631-379-0010 Fax: 631-689-0527 |
From: Jason R. <ja...@ho...> - 2009-03-18 15:56:11
|
All, We are NOT meeting at Sittercity's offices tonight, but rather Brillstreets offices. Details are as follows: 6pm BrillStreet 445 West Erie, Suite 200 Chicago, IL 60654 T: 312.421.2122 pizza and beer Speakers: Joe Dwyer - ??? Jason Rexilius - shmem functions and global counters, sems and locking |
From: Richard L. <ce...@l-...> - 2009-03-18 17:55:00
|
The location on the website has been updated, albeit at the 11th hour. I'll be there, but will have to bail out early, unfortunately. :-( On Wed, March 18, 2009 10:54 am, Jason Rexilius wrote: > All, > > We are NOT meeting at Sittercity's offices tonight, but rather > Brillstreets offices. Details are as follows: > > 6pm > BrillStreet > 445 West Erie, Suite 200 > Chicago, IL 60654 > T: 312.421.2122 > > pizza and beer > > Speakers: > > Joe Dwyer - ??? > Jason Rexilius - shmem functions and global counters, sems and locking > > > > ------------------------------------------------------------------------------ > Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) > are > powering Web 2.0 with engaging, cross-platform capabilities. Quickly > and > easily build your RIAs with Flex Builder, the Eclipse(TM)based > development > software that enables intelligent coding and step-through debugging. > Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com > _______________________________________________ > chiPHPug-discuss mailing list > chi...@li... > https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss > -- Some people ask for gifts here. I just want you to buy an Indie CD for yourself: http://cdbaby.com/search/from/lynch |
From: Trevor O. <tr...@gm...> - 2009-02-20 23:47:29
|
It seems like reasonable behavior to me. An empty string is a valid array value for explode. For example, if you exploded "||", it should return three empty strings. And from php.net: 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* . 2009/2/20 Arlo Leach <ar...@ar...> > 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 > -- -Trevor Oldak |
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 > |
From: Trevor O. <tr...@gm...> - 2009-02-20 23:52:54
|
It will return false if the DELIMITER is null, not if the string is null 2009/2/20 David Rovani <da...@ro...> > 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 > > > > > ------------------------------------------------------------------------------ > 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 > -- -Trevor Oldak |
From: Trevor O. <tr...@gm...> - 2009-02-20 23:52:47
|
It seems like reasonable behavior to me. An empty string is a valid array value for explode. For example, if you exploded "||", it should return three empty strings. And from php.net: 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* . 2009/2/20 Arlo Leach <ar...@ar...> > 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 > -- -Trevor Oldak |
From: Ryan G. <sok...@gm...> - 2009-02-20 23:53:58
|
If you explode an empty string, it makes sense to have a one element array, which consists of that same empty string. If you try count(array("")) , you will get a result of one, as an empty string is still an array element. There are a lot of different options for locating characters inside of a string, I'd probably go with strrpos. On Fri, Feb 20, 2009 at 1: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 > |
From: Wilfried S. <ws...@de...> - 2009-02-21 00:24:37
|
http://us3.php.net/explode: "If delimiter contains a value that is not contained in string , then explode() will return an array containing string." If you're using explode(), you're probably expecting an array. Seems normal to me.. On Feb 20, 2009, at 15:00 , Arlo Leach 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 |
From: Arlo L. <ar...@ar...> - 2009-02-21 03:21:43
|
Hello again, > If you're using explode(), you're probably expecting an array. Seems > normal to me.. Sure, I'm expecting an array, but I'm expecting it to be empty like the string it originated from. Let's say I get a field value from a database that is usually formatted like "1|2" but could also be "1" or "". If I leave it as a string, I can easily tell if it has data in it with a simple conditional. But if I explode the string to work with its individual elements, I can't easily tell anymore; count() returns 1 and empty() returns false even if the original value was "". I guess I could test for (implode(",", $array)), which returns false if the array consists only of one empty element. Or I could write a new function. Is there a better way? Since this comes up so often for me, it seemed like something that should be built in, but I'll settle for a more elegant workaround than the one I'm using now. Cheers, -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com |
From: Adam F. <am...@ad...> - 2009-02-21 04:37:32
|
It has never occurred to me to explode an empty string. Take a second and think "how would I implement explode()?" Would you test that string is not empty before proceeding thereby forcing an additional operation on every call to handle a fringe case? I would just test that my string is !empty() before proceeding if that's such a regular occurrence. --Adam -- Adam Finlayson am...@ad... |
From: Kenneth D. <ke...@se...> - 2009-02-23 12:08:31
|
Arlo Leach wrote: > Hello again, > > >> If you're using explode(), you're probably expecting an array. Seems >> normal to me.. >> > > Sure, I'm expecting an array, but I'm expecting it to be empty like the > string it originated from. > > Let's say I get a field value from a database that is usually formatted like > "1|2" but could also be "1" or "". If I leave it as a string, I can easily > tell if it has data in it with a simple conditional. But if I explode the > string to work with its individual elements, I can't easily tell anymore; > count() returns 1 and empty() returns false even if the original value was > "". > > I guess I could test for (implode(",", $array)), which returns false if the > array consists only of one empty element. Or I could write a new function. > Is there a better way? > > Since this comes up so often for me, it seemed like something that should be > built in, but I'll settle for a more elegant workaround than the one I'm > using now. > As I mentioned in my other email, if it is coming up often, I would ask how you are handling the array in downstream code. I have found it more robust overall to code loops that gracefully handle empty values, empty arrays, or arrays of empty values, etc. What I'd like to know more about is what you are doing with the array after you get it. > 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 > -- Kenneth Downs Secure Data Software ke...@se... www.andromeda-project.org www.secdat.com Office: 631-689-7200 Cell: 631-379-0010 Fax: 631-689-0527 |
From: Arlo L. <ar...@ar...> - 2009-02-23 17:45:18
|
Hi folks, Adam wrote: > Take a second and think "how would I implement explode()?" Would you > test that string is not empty before proceeding thereby forcing an > additional operation on every call to handle a fringe case? Well, yes, that's the situation I'm in now, testing the string before exploding to handle fringe cases. Before I adopt this as a personal coding practice to use every time, I'm just trying to find out if there's a more elegant way to handle it. Kenneth wrote: > I have found it more robust overall to code loops that gracefully handle > empty values, empty arrays, or arrays of empty values, etc. That's a good point. My expectation originally was that I wouldn't even enter the loop, but now that I know this detail of the explode() behavior, I can pay more attention to that. And again: > What I'd like to know more about is what you are doing with the array > after you get it. Well, here's an example. After members register on a social networking platform, they have to select one or more roles from a list. That's stored in the db with the pipe delimiters (and regarding Rich's suggestion, I didn't build the platform, so I don't get to decide how the data is stored). When members come back and log in later, the login script gets that value, explodes it, and puts it into a session variable for use on other pages. The "fringe case" is that sometimes members will abandon on the selection page, but still come back later and log in. In this case the "roles" field in the db is empty, but when that's exploded, it creates an array with one element. Now, a few pages later, I have a conditional that says, "if members have just one role, show content specific to that role; if they have multiple roles, just show some general content." If I count the array, I think that the member has one role, so I try to show the content for $roles[0] -- but $roles[0] is empty, so I get an error that there's no content for that role. Obviously, once I identified this particular problem, I had a few different options for addressing it. I could check the roles field on subsequent logins, I could check the $roles[0] value before displaying the data, etc. The most intuitive for me would be writing that conditional to distinguish between members with no roles, members with one role, and members with multiple roles -- but I don't see any built-in function that distinguishes between array("") and array("1"). I would have to write a custom function or do something funny like evaluate the result of (strlen(implode("|", $roles))). I'm just looking for the simplest approach to adopt that will work in the widest number of cases, to avoid getting bit by a fringe case I didn't anticipate. All the cases I've run into so far would have been avoided if array("") had been array(), so I might just stick with my original solution: $array = ($string) ? explode("|", $string) : array() ; Or possibly a custom function that moves that extra clutter out of my main code. Cheers, -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com |
From: Richard L. <ce...@l-...> - 2009-02-21 04:54:32
|
I would use strlen on it, rather than just ($string) personally... If $string happens to contain just "0" then ($string) would probably not behave as you desire. For the case of pulling 1|2|3 from the DB, I'd suggest you consider a DB data structure that doesn't make you use PHP explode in the first place :-) SQL arrays or bit-masked INT or ??? Another option is to just use the array you have, but skip any empty values as you loop through them. One reason why explode does what is does is so that it is the exact reverse of implode (and vice versa) What would you expect these to output: implode('|', array('foo', 'bar', 'baz')); implode('|', array('foo')); implode('|', array('')); As you can see, if that last one is to do what you expect, and implode/explode are supposed to be exactly opposite, then explode('') must return an array with a single empty string element. On Fri, February 20, 2009 3:00 pm, Arlo Leach 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 > -- Some people ask for gifts here. I just want you to buy an Indie CD for yourself: http://cdbaby.com/search/from/lynch |
From: Arlo L. <ar...@ar...> - 2009-02-23 17:45:21
|
Hello again, Rich wrote: > One reason why explode does what is does is so that it is the exact > reverse of implode (and vice versa) > > What would you expect these to output: > > implode('|', array('foo', 'bar', 'baz')); > implode('|', array('foo')); > implode('|', array('')); Very interesting. But here's one more test case; what would you expect this to return? implode('|', array()); An empty string, right? Then, by the same logic, it would be equally correct for explode('|', '') to return array() as array(''). Cheers, -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com |
From: matt d. <mm_...@ya...> - 2009-02-23 19:44:09
|
Wouldn't it be easier to address the bad (actually empty) data going in? Or clean in up at some point? ________________________________ From: Arlo Leach <ar...@ar...> To: Chicago PHP User Group <chi...@li...> Sent: Monday, February 23, 2009 11:18:25 AM Subject: Re: [chiPHPug-discuss] troublesome PHP behavior Hello again, Rich wrote: > One reason why explode does what is does is so that it is the exact > reverse of implode (and vice versa) > > What would you expect these to output: > > implode('|', array('foo', 'bar', 'baz')); > implode('|', array('foo')); > implode('|', array('')); Very interesting. But here's one more test case; what would you expect this to return? implode('|', array()); An empty string, right? Then, by the same logic, it would be equally correct for explode('|', '') to return array() as array(''). 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 |