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("pgsql.so"); //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
(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!
jeffa
Blessed is the PHP programmer who grokketh Perl.
| [reply] |
|
| [reply] |
|
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.
HTH
_________ broquaint | [reply] [d/l] |
|
|
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.
| [reply] |
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. | [reply] |
|
More bad stuff:
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.
| [reply] [d/l] |
|
| [reply] |
|
|