Stories
Slash Boxes
Comments
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

use Perl Log In

Log In

[ Create a new account ]

petdance (2468)

petdance
  andy@petdance.com
http://www.perlbuzz.com/
AOL IM: petdance (Add Buddy, Send Message)
Yahoo! ID: petdance (Add User, Send Message)
Jabber: petdance@gmail.com

I'm Andy Lester, and I like to test stuff. I also write for the Perl Journal, and do tech edits on books. Sometimes I write code, too.

Journal of petdance (2468)

Tuesday January 06, 2004
03:41 PM

Another reason I hate PHP

[ #16675 ]
In Perl, if you split an empty string, you get back zero elements:

my @foo = split( ":", "" );
print Dumper( \@foo );

# $VAR1 = [];

In PHP, if you split an empty string, you get back a one-element array with 0 as the key and an empty string for the value.

$foo = explode( ":", $str );
print_r( $foo );

$foo = split( ":", $str );
print_r( $foo );

$foo = preg_split( "/:/", $str );
print_r( $foo );

... in all three cases prints

Array
(
    [0] =>
)

This of course means that I must explicitly check to see if the value on my first element is blank, or must myself check to see if the string I'm splitting is blank.

frickin' frackin' grambl-grrrrr...

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
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) ) {
         

      --

      --
      xoa

      • 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 [juerd.nl] 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 (http://martynov.org/ [martynov.org])

  • The answer perl is giving you seems to be right to me. If you try split( /:/, "a:b::c" ) you get a 4-element result with the third being the null string, ("a", "b", "", "c"). For a single element result, split( /:/, "ab" ) returns the single element "ab", split( /:/, "a" ) returns the single element "a", and split( /:/, "" ) returns the single element "". Returning 0 elements for the last case would be inconsistant with the previous set of results.