PHP vs perl
if you have a php, programming, or open source related blog, email me your url here: firstname.lastname@example.org
PHP and perl are both powerful languages used successfully in a server environment. Here are some brief differences between the two languages:
- PHP is built from the ground-up with database functionality built in, particularly MySQL functionality. Perl is not.
- PHP code gets embedded into HTML pages, unlike Perl. This makes it very fast to code web pages and fast to deploy a new site, thus speeding up Web development and lowering overall cost of ownership. An important code management technique for programmers is separating code from data. This allows us to make changes to the code or data without affecting the other. PHP uses the tags to indicate "code inside". In Perl, however, programmers are encouraged to use print statements to generate the HTML. True it is possible to implement templates in Perl (with more difficulty than in PHP) to separate code and HTML, but 90% of sample Perl code on the web doesn't do that.
- PHP is secure. Perl scripts tend to have more security holes. This is because PHP has built-in a lot of the internal operations of dealing with web page requests and serving information.
- PHP is easy to learn in comparison to Perl. It's easier to learn than C, Python, Java, and most other programming languages used in web development, for that matter. The Perl style of programming is unique, and thus not universally applicable to or from other programming languages. Accessing web form variables in PHP is straightforward, but in Perl requires either detailed knowledge of either HTTP header formats or one of many Perl CGI libraries.
- PHP takes less "overhead" than Perl, meaning that PHP scripts will run faster than CGI scripts written in Perl, and you'll be able to handle more simultaneous users on your site.
- PHP code tends to be more consistent and modular than Perl.
A few of them are listed below (the rest can be found at the website listed above):
PHP has separate functions for case insensitive operations
(This can be argued both ways. Some think it's good to have functions that combine functions, even if that means having dozens of extra names to remember)
In Perl, you use a double lc() (lowercase) or the /i flag where PHP usually provides a case insensitive variant. The case-insensitive versions have very inconsistent naming.
Perl: $foo cmp $bar lc $foo cmp lc $bar
PHP: strcmp($foo, $bar) strcasecmp($foo, $bar)
Perl: index($foo, $bar) index(lc $foo, lc $bar)
PHP: strpos($foo, $bar) stripos($foo, $bar)
Perl: $foo =~ s/foo/bar/ $foo =~ s/foo/bar/i
PHP: $foo = str_replace('foo', 'bar', $foo) $foo = str_ireplace(...)
PHP: $foo = ereg_replace('foo', 'bar' ,$foo) $foo = eregi_replace(...)
PHP has inconsistent function naming
- Case insensitive functions have the 'i' or 'case' at different positions in their names.
- There is no apparent system in underscore(s) versus no underscore(s):
underscore no underscore:Perl has no core function names with underscores in them.
- PHP has unlink, link and rename (system calls), but touch (the system call is utime, not touch).
- And they can't decide on word order:
- object verb: base64_decode, iptcparse, str_shuffle, var_dump
- verb object: create_function, recode_string
Perl core functions are all "verb object" except the superseded dbm* functions. (Note that sys is a prefix, not an object. And that flock and lstat were named after the system calls. shm* and msg* are library calls)
- "to" or "2"?
ascii2ebcdic, bin2hex, deg2rad, ip2long, cal_to_jd (jdto*, *tojd), strtolower, strtotime,
PHP has no lexical scope
Perl has lexical scope and dynamic scope. PHP doesn't have these.
For an explanation of why lexical scope is important, see Coping with Scoping.
Superglobal Yes Yes 
Global Yes Yes
Function local Yes Yes 
Lexical (block local) No Yes
Dynamic No Yes
 Perl has variables that are always in the main:: namespace. These are like PHP's superglobals.
 Using a lexical variable in a sub routine's block, you get a function local variable.
PHP has too many functions in the main namespace
(Using the core binaries compiled with all possible extensions in the core distribution, using recent versions in November 2003.)
Number of PHP main functions: 3079 
Number of Perl main functions: 206 
Median PHP function name length: 13
Mean PHP function name length: 13.67
Median Perl function name length: 6
Mean Perl function name length: 6.22
Note that Perl has short syntax equivalents for some functions:
readpipe('ls -l') ==> `ls -l`
glob('*.txt') ==> <*.txt>
readline($fh) ==> <$fh>
quotemeta($foo) ==> "\Q$foo"
lcfirst($foo) ==> "\l$foo" (lc is \L)
ucfirst($foo) ==> "\u$foo" (uc is \U)