Perl vs PHP (a bit of credit to PHP)

Just read this blog post. Comments are disabled, so I thought I'd add a blog post.

There are endless ways we can sneer at PHP's deficiencies, but since 5.3 PHP already supports anonymous subroutines, via the function (args) { ... } syntax. So:

$longestLine = max(
    array_map(
        create_function('$a', 'return strlen($a);'), 
        explode("\n", $str)
    )
);

can be rewritten as:

$longestLine = max(
    array_map(
        function($a) { return strlen($a); }, 
        explode("\n", $str)
    )
);

though the example is not a good one since it might as well be:

$longestLine = max(
    array_map(
        'strlen',
        explode("\n", $str)
    )
);

5 Comments

Why do we give kudos to PHP for that?

Fair enough

You should use mb_strlen($str, "utf-8"); if you want multibyte support.

Yeah but part of the problem is that 5.3 is not compatible with 5.2, so many companies (this one, for example) cannot afford to upgrade.

If they'd released additions as 5.3 and deprecations as 5.4 we could have got the benefits of it, but since the PHP gods don't see it worthwhile catering to the desires of their slaves, a lot of us will be stuck with 5.2 until we can afford the developer time needed to upgrade without angering a lot of clients.

:(

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.