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...
Why? (Score:1)
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)
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:
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:
--
xoa
Re:Why? (Score:1)
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:
The @ will suppress errors, sin
It's like grep, but not (Score:2, Funny)
broquaint out
Re:Why? (Score:1)
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:
Re:Why? (Score:1)
Re:Why? (Score:1)
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
Re:Why? (Score:1)
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):
Darn it! (Score:2, Funny)
Couldn't you at least say something nasty about Perl?
Re:Why? (Score:3, Insightful)
Ilya Martynov (http://martynov.org/ [martynov.org])
perl is right here (Score:2)