PHP Versus Perl

Comparing PHP scripting with CGI scripting using Perl


PHP Vs Perl

by Christopher Heng, thesitewizard.com

Update: This article was written in 2000 and compares two programming languages for the purposes of introducing PHP. As such, to understand it, you need some programming background. When I first wrote the article, a lot of the webmasters that visited thesitewizard.com knew Perl, and PHP was the new kid on the block. It was from that perspective that this article was written. This is of course no longer true today; nowadays, the majority of webmasters are probably not even remotely connected to the computer industry (except that they own websites). If you are not a programmer, you should probably read the article What is HTML? What are PHP and Perl? instead, since it was written with the layperson in mind. And if you are an experienced web developer, you already know PHP, and this article, written in the early days of the language is both unnecessary and too cursory a discussion for you.

PHP seems very much in vogue now — with most web hosts providing support for it. For those who have only vaguely heard of it and are not too sure what it is, this article discusses PHP and informally compares writing PHP scripts with writing CGI scripts in Perl.

PHP is a free server side scripting language. It can be built into web servers like Apache and you can use it to generate your pages dynamically. You will probably use it in situations you would have otherwise used a Perl CGI script for. For example, the PHP feedback form generated by thesitewizard.com's Feedback Form Wizard can be used by visitors to your website to send feedback to you.

The Language

If you are coming from a C, C++, Perl, Java or JavaScript background, learning PHP would probably be a piece of cake. In fact, you probably can get started writing your scripts almost immediately.

It uses typeless variables the way Perl does, prefixed with a "$" sign and holding any data type you wish. For example, $whatever can be a variable that you can use to contain strings, numbers, whatever. If $whatever contained a number, you can increment its value using

$whatever++ ;

or

$whatever += 1 ;

or

$whatever = $whatever + 1 ;

Doesn't it remind you of Perl, C, C++, Java, JavaScript? See what I mean?

Built-in Facilities

Unlike Perl, which is a general purpose scripting language that you can use for a wide variety of purposes (and not just generating web pages), PHP was designed from the ground up to be used for scripting web pages. As a result, it has lots of facilities built into that you may have to write yourself or use some pre-written module if you were using Perl.

For example, do you want to send email to yourself from a form on the web page? In Perl, one common way to do this is to use code like the following:

open ( MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: myself\@example.com\n" ;
print MAIL "From: visitor\@example.com\n" ;
print MAIL "Subject: Comments from Web Form\n\n" ;
print MAIL $mainmessage ;
close ( MAIL ) ;

In PHP, the same thing would be coded as follows:

mail ( "myself@example.com", "Comments from Web Form",
  $mainmessage, "From: visitor@example.com" );

Nifty, huh? The same goes for other facilities like sending or retrieving a document via HTTP or FTP, etc. Since PHP was specially designed for a website, the facilities that web designers typically want in a scripting language are built into it.

Another convenience is its handling of form input. Take for example a form with a field like:

<input type="text" name="dateofbirth">

The data submitted for that field is made available to your script in the array variable $_REQUEST['dateofbirth']. You can assign its contents to any variable you like, or use it directly. There's no need to parse form inputs or the like. All fields in the form are automatically converted to variables that you can access.

Accessing databases is just as easy. There are built-in facilities in PHP to access MySQL, MSQL, Dbase, Oracle, InterBase, and so on (the list is very long). Need to MIME encode your message? There's a function to do it for you too.

There are many more features. I obviously can't run through the entire list — it would take a whole book to be exhaustive. This is just to whet your appetite.

Generating web pages

By default anything you type in your PHP document is given verbatim to the web browser. So a simple PHP script might look like the following:

<!DOCTYPE HTML>
<html>
<head><title>My First PHP Script</title></head>
<body>
<h1>My First PHP Script</h1>
<p>
Welcome, Internet user from IP address
<?php echo $_SERVER['REMOTE_ADDR']?>.
Hope you like my first PHP page.
</body>
</html>

Notice that it looks exactly like a web page, except for the <?php ... ?> bit, which encloses the PHP script. In this case, all we want is for the script to output the visitor's IP address to the page, hence we use the "echo" function. The web server's environment variable REMOTE_ADDR is automatically made available to the PHP script via an array variable $_SERVER['REMOTE_ADDR']. In fact, all server environment variables are available to the PHP script in the $_SERVER array variable. So, for example, the name of your visitor's browser is available in $_SERVER['HTTP_USER_AGENT'].

There are many ways to embed your PHP script into your page, or to design your page itself. But you've probably already got the general idea. As I said, PHP was designed for web pages, so the idea of output to the server is built into its design. It makes writing such scripts a very pleasant task.

What's the Catch?

While I obviously enjoy using PHP as my web scripting language, I do not claim that it is the perfect solution for all your website needs.

You might want to consider the following prior to committing yourself to it. The list, incidentally, is not exhaustive.

  1. Not all web hosts provide PHP facilities. Most do nowadays, however, so if yours does not, it's simply a matter of changing web hosts to one that does.

  2. Like all web scripting languages (Perl included), debugging the script can be a pain in the neck unless you download and install your own copy of PHP. Otherwise you might spend many hours online trying to test and debug your script (unless of course it's a trivial script). Instructions for how to install it on Windows can be found in my article How to Install and Configure Apache, PHP, Perl and MySQL on Windows the Easy Way (with XAMPP).

    If you have a Linux box around, you can just get Apache and all its modules via your distribution's package manager.

  3. It probably cannot beat Perl in terms of convenient and efficient text crunching. Let's face it: Perl is designed with crunching text in mind and has facilities for handling strings and the like that put most other languages to shame. However, PHP does have adequate facilities for most web purposes.

Where to Get It?

Most, if not all, of the Unix web hosts listed on thefreecountry.com's Budget Web Hosting have PHP support.

There are probably a few hosts listed on the Free Web Hosts page that support PHP. Note however that some free hosts disable certain PHP functions (like the mail() function) to prevent its abuse by spammers.

The entire PHP documentation set comprising the reference manual for the various PHP language features and functions can be downloaded from the PHP web site at https://www.php.net/. The sources and binaries for PHP can also be found on the same site should you wish to run a copy on your own machine for testing purposes.

If you're interested in getting started with PHP, you might want to check out the following tutorials found on thesitewizard.com:

By the time you read this, there'll probably be other instalments of this PHP tutorial series. You might want to check up the main index of PHP articles yourself, at https://www.thesitewizard.com/php/index.shtml

Copyright 2000-2019 by Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/

thesitewizard™ News Feed (RSS Site Feed)  Subscribe to thesitewizard.com newsfeed

Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.

Related Pages

New Articles

Popular Articles

How to Link to This Page

It will appear on your page as:

PHP Vs Perl





Home
Donate
Contact
Link to Us
No Spam Policy
Privacy Policy
Topics
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Domains
Web Hosting
Blogging
JavaScripts
PHP
Perl / CGI
HTML
CSS
.htaccess / Apache
Newsletters
General
Seasonal
Reviews
FAQs
Wizards

 

 
Free webmasters and programmers resources, scripts and tutorials
 
HowtoHaven.com: Free How-To Guides
 
Site Design Tips at thesitewizard.com
Find this site useful?
Please link to us.