Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

PHP vs Perl code compare

by mandog (Curate)
on Oct 28, 2002 at 00:03 UTC ( #208414=perlquestion: print w/replies, xml ) Need Help??

mandog has asked for the wisdom of the Perl Monks concerning the following question:

Some folks I respect are enthusiastic about PHP. I wrote the same toy script in both perl and PHP. So far I'm not sharing the love. Maybe it is because I've only got two hours experience with PHP?

Comments? I know there is at least one monk with a php site.

#!/usr/bin/php4 <?php dl(""); //kludge will be configured in // global php.ini soon $word=$HTTP_POST_VARS['word']; # no password needed as we are using suexec # and ident server locally $conn=pg_pconnect("user=mandog dbname=mandog"); if (!$conn) { print "bad databae connection!"; exit; } $query="INSERT INTO hello VALUES ('$word');"; $result=pg_exec($conn,$query); $query="SELECT word,to_char(when_hello,'Month DD HH24:MI:SS') FROM hel +lo;"; $result=pg_exec($conn,$query); if (!$result) { print "An error occured.\n"; exit; } $num = pg_numrows($result); print "<PRE>"; print "words of the moment\n"; for ($i=0; $i < $num; $i++) { $r = pg_fetch_row($result, $i); print "on: $r[1] the word was: \t$r[0]\n"; } print "</PRE>" ?> #!/usr/bin/perl -wT use strict; use CGI; use DBI; my $cgi=new CGI; print $cgi->header(); my $word=$cgi->param('word') || 'nothing entered for perl'; # RaiseError instead of manually dying # no password needed as we are using suexec and ident server locally my $dbh=DBI->connect('dbi:Pg:dbname=felicia','felicia', "",{ RaiseErro +r => 1}); my $query="INSERT INTO hello VALUES (?);"; my $sth=$dbh->prepare($query); $sth->execute($word); $query=$query="SELECT word,to_char(when_hello,'Month DD HH24:MI:SS') F +ROM hello;"; $sth=$dbh->prepare($query); $sth->execute(); print '<PRE>'; while (my @r=$sth->fetchrow_array()){ print "on: $r[1] the word was: \t$r[0]\n"; } print '</PRE>'; $dbh->disconnect();

email: mandog

Replies are listed 'Best First'.
(jeffa) Re: PHP vs Perl code compare
by jeffa (Bishop) on Oct 28, 2002 at 00:32 UTC
    I find comparing two languages with "toy scripts" to not be very exciting. You are not going to really see the strengths and weaknesses until you delve deeper. Consider how database and interface abstraction layers influence your decision. That's where the excitement begins. ;)

    I have been coding in PHP for the past week and so far ... it's not so bad. I have been using the Smarty templating engine and have been having very good results.

    Why am i using PHP? First, because the original site was written with it. Second, my client wants PHP. Personally, I would rather use mod_perl, but (for me so far) PHP has been more fun than JSP, ASP, and Cold Fusion combined. If you are serious about PHP, i recommend O'Reily's Programming PHP and Smarty. PEAR looks promising as well.

    What have i found the differences/similarities between Perl and PHP to be so far?

    • like Perl, PHP has array and associative array support, anonymous subroutines, built-in session support, just about everything a serious web monkey needs
    • PHP has regex support, but it's not built in like Perl's.
    • PHP is easy - this is good and bad. Good because it allows novice programmers to code powerful stuff. Bad because it allows novice programmers to code powerful stuff.
    • PHP has PEAR, PEAR ain't no CPAN
    All in all, i can't really knock PHP, but if i have a choice - mod_perl!


    Blessed is the PHP programmer who grokketh Perl.

      Just one thing: You say PHP supports anonymous subroutines, but sadly it doesn't.

        It doesn't quite have the same flexibility as perl's anonymous subs but there is an equivalent in PHP
        <?php $f = create_function('$s', 'echo "got: $s";'); $f("a string"); ?> __output__ got: a string
        So we have an anonymous function but unfortunately it is isn't totally orthogonal with normal functions in PHP.


      I have no problems with PHP as a language, I learned it before I learned Perl, and it has its own strengths. (Readability, designed for web apps) However, when it comes to making use of existing code modules, nothing I've seen can touch CPAN. Frankly CPAN has spoiled me considerably, I find locating PHP modules to be an extremely irritating task now.

      Speaking of which, I spent quite a bit of time a few weeks ago looking for a good template module for PHP, and was finally delighted to find vLib Templates. I'm not sure how its caching speedup compares to Smarty's, but it had the impressive bonus of looking very familiar.
Re: PHP vs Perl code compare
by mbadolato (Hermit) on Oct 28, 2002 at 05:01 UTC
    Having been forced to use PHP for the past year or so, I have to say that the more I use it, the more I get annoyed with it. Some of my gripes:

    The OO support absolutely sucks. No privitization of variables. No multiple inheritance. It's a performance killer.

    Too many damn functions to do every little thing. I'm surprised they don't have a sort_an_array_by_the_values_second_character_if_it_is_after_2_pm() function.

    no built-in database extraction

    ever changing syntax. from minor point release to minor point release, functions change and break code (look at the manual for entires like "<4.0.3 use xxx. >= 4.0.3 use xxx"). That's damn obnoxious

    Socket support keeps changing. Granted it's marked as an experimental module, but the interface has changed between 4.06 and 4.1.x, and between 4.1.x and 4.2.x. It's annoying. We can't upgrade to the latest php because socket use in all our code will break.

    Doesn't have Perl's felxibility.

    Regex work is clumsy

    no map and grep (actually i hear that something similar may now be in there as of a few point releases ago, i havent checked)

    that's a few that come to the top of my mind. PHP does have good points too, but to me it's more of a toy language at times.

      More bad stuff:
      • Basically only two kinds of variable scope: global and local to a function. That means that if you have several PHP files that are intended to be used together, and just "do things" that are not wrapped inside a sub 'er, "function"), that they'll stamp on each others variables. There's no way to limit a variable's scope to "just this block" or "just this file".
      • Guess what value the variable $x will have after the loop finishes?
        # PHP code $x = 'foo'; $row = array('a', 'b', 'c'); foreach ($row as $x) { echo "$x\n"; }
        I'll tell you: loop variables are not localized. Stomp stomp stomp.
      • No support for placeholders in database access *. That means that you have to produce literal SQL strings to execute, by incorporating the data in the SQL statement. It's slow if you have to do this many times in a loop. But, in order to prevent people form accidently creating invalid SQL this way, GET and POST values are pre-addslashes()-ed by default! Talk about doing a patchup in the wrong place!

      Update: broquaint pointed out that PHP does have support for placeholders, which is a good thing. I must say that none of the PHP developers I've spoken to, has ever even heard of them. So use of placeholders in PHP doesn't seem very commonplace. However, my complaint on the automatic addition of backslashes on special characters in form variables still stands, which was my main gripe in that point.

      The phrase "It has about as much support for placeholders as perl does natively" is one I just cannot grok. Perl does not have native SQL database support without DBI. DBI is the way to access SQL databases in Perl. Using placeholders is one of the first things you learn there. OTOH, AFAIK they're hardly even mentioned on PHP's manual website, and only under Oracle and ODBC. Don't you just hate it that every database has its own kind of access functions.

        No support for placeholders in database access
        It has about as much support for placeholders as perl does natively. But if you're using the PEAR DB class then you can use placeholders as you would with perl's DBI.

        While I'm all for pointing out PHP's flaws as a language (which I am acutely aware of having used it extensively for 2 years) I'm just as wary as any language advocate about spreading false information.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://208414]
Approved by graff
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2023-09-21 08:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found