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.
  • by shiflett (3841) on 2004.01.06 16:28 (#27082) Homepage Journal

    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 elements in the array are the pieces of my original string, regardless of what the string may have been. I'd rather a language not try to guess my intentions or decide that I would like my empty string converted to an empty array.

    $string = 'foo:bar';
    $foo = explode(':', $string);
    $bar = explode(',', $string);


    In this case, both $foo and $bar are arrays, even though a comma does not exist in the string. This outputs:

        [0] => foo
        [1] => bar
        [0] => foo:bar

    It makes sense to me that $bar is an array with a single element, [0] => foo:bar. For the same reason, it makes sense to me why using explode() on the empty string would do the same.

    • 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) ) {



      • 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

    • In addition, PHP's implementation makes more sense in this case

      In practice, I've found perl's behavior in this regard convienient. If for some reason I want PHP-like behavior, I can always add a one liner after the split:

      @array = ('') unless @array;
      • I'm always happy to listen to the arguments against PHP...

      And, I'm willing to listen to arguments for PHP. What does PHP buy you that you can't get with things like plp [] or the various templating systems?

      I've never done any PHP myself, but those I know who have used it are constantly frustrated by the limitations and often resort to Perl on the backend. Why have two languages? The maintenance and support nightmares multiply in that kind of environment, from what I can see.

      Maybe PHP had a place once up

      • I'm not one to ever argue for PHP over mod_perl or the other way around. To me, both are winners, and you should go with what you're familiar with. My arguments against languages involve all of the stuff that begins with J and all of the stuff made by Microsoft. :-)

        Here are some reasons why PHP is so popular (mod_perl shares some but not all of these features):

        1. It's an open source, server-side scripting language made for creating Web applications.
        2. The syntax is very similar to C, so for many programmers,
        • Here I was all set to start a big religious flame war over Perl vs. PHP and you douse it with a cold bucket of reasonableness and solid justification.

          Couldn't you at least say something nasty about Perl?

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

        I think the main reason for PHP popularity is that it is the most ISP friendly technology. They have builtin security mechanisms that allow many users to share one server (what is impossible with mod_perl). PHP have so many things builtin what makes it usable platform without installing dozen CPAN modules what somewhat lowers support costs for ISP (no users which request to install module Foo and module Bar). PHP as platform is just cheaper for ISPs to support. Having said this I must admit that after tryin

        Ilya Martynov ( [])