Friday, December 7, 2007

PHP Vs Perl

1. 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?

2. 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\@mydomain.com\n" ;
print MAIL "From: visitor\@hisdomain.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@mydomain.com", "Comments from Web Form",
$mainmessage, "From: visitor@hisdomain.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:

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're 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.

3. 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:


My First PHP Script

My First PHP Script



Welcome, Internet user from IP address
. Hope you like my first
PHP page.


Notice that it looks exactly like a web page, except for the 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.

No comments: