Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I don't really see why this is bad (feel free to show me the errors of my ways, heh). Why should whether PHP behaves like Perl have any bearing on its merits as a language? I'm always happy to listen to the arguments against PHP, and there are some good ones, but this seems a bit trivial to me.

    In addition, PHP's implementation makes more sense in this case, especially since it is consistent with other PHP functions. If explode() returns an array, I can be confident that I always get an array, and the eleme

    • Re:Why? (Score:3, Informative)

      An empty array is still an array, so why not allow that?

      Here's what I was doing. I had a string of codes that looked like this: "2 4 7". So I expected to be able to do:

      $codes = split( " ", $str );
      foreach ( $codes as $code ) {
          print $lookup[$code];
      However, if $str is empty, instead of going thru the loop zero times, which is what I would expect, I go through once and now have an error because "" is not in my $lookup array. Instead, I have to do this:
      if ( !empty($str) ) {
          $codes = split( " ", $str );
          foreach ( $codes as $code ) {
              print $lookup[$code];
      Zero/empty is a perfectly valid construct. explode() et all should support it.


      • Your example makes a lot more sense, and I think I understand your complaint better now. :-)

        I guess that I've never encountered this type of situation, because I am usually dealing with data that originates from the user. As such, I would be checking what's in $str (or checking each element in $codes) to make sure that it's a valid key for $lookup.

        You might prefer to do this:

        $codes = explode(' ', $str);
        foreach ($codes as $code)
             @echo $lookup[$code];

        The @ will suppress errors, sin

      • It's a simple matter of using PHP's wonderful approximation of grep ...
        foreach( array_filter(explode(",", ""), create_function('$a','return !empty($a);')) as $v ) {
          echo "got: $v\n";
        Or not ...

        broquaint out