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 |