javascript hit counter Web World: PHP vs CGI
Nikhil Sheth

Web World

This site is for php , mysql , flash,.... Inshort for any web related stuff

Friday, May 13, 2005

PHP vs CGI

PHP seems very much in vogue now - with an increasingly greater number of 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 it 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 would probably use it in situations you would have otherwise used a CGI script for. For example, thefreecountry.com's Feedback form (among other things) uses a PHP script to generate the form and send the message to me.

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 (I did).

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 ;

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, you probably would have to code something 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:



You can immediately access that field with the $dateofbirth variable. No need to parse form inputs and 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 lots more. 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:

< html >
< head >< title >My First PHP Script< /title >< /head >
< body >
< h1>My First PHP Script< /h1>


Welcome, Internet user from IP address
. Hope you like my first
PHP page.
< /body>
< /html >

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 a variable of the same name (as are all other environment variables and form inputs).

There are many ways to embed your PHP script into your page, or to design your page itself. But you 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.

4. Debugging With PHP Vs Perl CGI

Interestingly, if you're debugging your scripts online, PHP really shines.

Normally, when a Perl CGI script goes awry, you'll get a cryptic error message in your browser: something to the effect of "500 Internal Server Error".

When your PHP scripts online, you get error messages pinpointing the offending lines in your code to help you locate the error. However, the message is sometimes a cryptic "parse error" or the like, so you still have to crack your head to figure out the problem. But at least you know where it occurred. With Perl CGI scripts, an "Internal Server Error" could have arisen from any number of causes, from a syntax error to a simple case of forgetting to make the file executable or uploading it in text mode.

Debugging offline, however, is another story. Some people have found that the Perl interpreter gives more helpful messages than the PHP interpreter, which tends to label many things as "parse error". This may change, though, as newer versions of the PHP interpreter is released.

4. 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 ot it. The list, incidentally, is not exhaustive.

a. Not all web hosts provide PHP facilities. While it is true that many also do not provide CGI access, the number providing PHP is even less!

In fact, where free web space providers are concerned, the number providing PHP can probably be counted with one hand. Indeed, even if you manage to find free web hosting with PHP access, you have to ask yourself whether you really want to depend on it for your site. There might be a day when you need to move your site, and you may be hard-pressed to find another free web host that supports PHP.

However, if you host with commercial web hosting companies, you probably will have less problems. It seems to me like the large majority of vendors support PHP, and even those who currently don't provide it plan to support it in the near future.

b. 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 PHP on Windows" at:

http://www.thefreecountry.com/articles/installphp.shtml

Incidentally, you can also operate your own Apache web server at home, so as to mimic the entire environment of your actual site (or as close to it as necessary). You can read all about setting up your own Apache, if you have a Windows machine, from my other article "How to Install Your Own Apache Server on Windows 95/98/NT/2000" at:

http://www.thefreecountry.com/articles/apache.shtml

Of course if you have a Linux box around, you're probably all set. Just dig up your installation CDROMs and install the server and PHP module from there if you've not already done so. (Most modern Linux distributions come bundled with the Apache server and PHP Apache module.)

c. It is not a general purpose language. While it has many facilities specifically catered towards web programming, it is not Perl (or C or C++ or Java). I personally however find PHP more than adequate for my web programming needs.

5. Where to Get It?

A number, if not most, of the Unix web hosts listed on our Budget Web Hosting and Best Web Hosting pages already have PHP support. You can find them at the following URLs:

http://www.thefreecountry.com/ecentricity/bestwebhosts.shtml
http://www.thefreecountry.com/ecentricity/budgethosting.shtml

There are probably a few (very few) hosts listed on our Free Web Hosting pages that support PHP. You can check them out yourself if you don't want to pay for web hosting:

http://www.thefreecountry.com/ecentricity/hosts.html

You should also download the entire PHP documentation set from the PHP web site. At the time of this writing, there appears to be no tutorial to get you started. Perhaps I might put up one on thefreecountry.com when I have time.

The PHP web site can be found at:

http://www.php.net/

You can also get sources and binaries for PHP there, if you wish to run a copy on your own machine for testing purposes. Note that you don't have to have a web server to run PHP on your machine, although it is probably more convenient to have it. On Unix based systems PHP comes either as an Apache module or a separate binary. On Windows, PHP is implemented as a separate executable. You can just run the executable versions of PHP on your scripts offline. You'll probably want to set up your environment properly and strip the headers from the output to more closely mimic your actual web site.

Happy scripting!

Here are some additional helpful resources on PHP, hand picked by the authors of JavaScript Kit:

1) PHPBuilder.com
2) DevShed PHP Tutorials
3) TheScripts.com PHP Tutorials
4) Building a database driven site using PHP (Sitepoint.com)
5) WeberDev.com

14 Comments:

  • At 6:36 PM, Blogger Oren Almog said…

    There is a great, free php book online. http://www.hudzilla.org/phpbook/index.php

     
  • At 1:14 PM, Blogger Subhendu said…

    You will get good tutorials and free PHP scripts to downlaod and discuss installation problems etc at PHP forum. visit www.plus2net.com

     
  • At 7:01 AM, Anonymous net web hosting said…

    Sad to say I just got back from a bowling tournament and decided to log in and do some websurfing. Niks I love your blog. I had some very good laughs. I am doing a paper on best web hosting company and have been downloading information for the last hour. I don’t know how I came across PHP vs CGI but I am glad I did. It has set me back a little because I have spent the last hour reading your archives. If you don’t mind I would like to add you to my favorites so I can back again and read some more. Well I need to get back to best web hosting company. I am almost finished with it. Great job.
    p.s some very good points on your blog

     
  • At 6:02 AM, Anonymous net web hosting said…

    All I can say is WOW Niks. The other half and I just got back from our friends house (well her friends house) and I needed a huge break. I am working on a project right now that is based on web hosting company for small business. I have literally been on-line for 2-3 hours doing research. Even though PHP vs CGI really isn’t on the same page as web hosting company for small business I am certainly glad I came across your blog. There are a ton of great view points on this blog. Well I think I can here the kids screaming in the background. I put you in my internet favorites and I will certainly come back and visit. If you want to take a peek at my site you can find me here at web hosting company for small business. I update my site very frequently. Again, great job blogging and I will be back again soon!

     
  • At 7:33 PM, Anonymous commercial web hosting said…

    I have been on-line searching for hours for information regarding net web hosting and stumbled across your blog during my journey :-) Niks your blog is really amazing! Keep up the great work. Obviously my search on net web hosting was way off when compared to PHP vs CGI and find it funny how it landed me here. The internet is a funny thing. Anyways, great job on your blogging and keep up the good work! I been searching for net web hosting for over 2 hours and needed a break from it. I started reading your blog and really started getting into it.
    P.S I will add you to my favorites so I can come back and visit later
    P.S.S If you want to bookmark my site I am at net web hosting. You never know you may find some good deals!

     
  • At 7:28 PM, Anonymous commercial web hosting said…

    Hi Niks your blog is really great! Wow :-) As I was out blog surfing and surfing the web for detailed info on small business web hosting services I stumbled across your blog. Obviously my search landed me here and it is a little off subject compared to PHP vs CGI, but I am certainly glad I did come across your blog. Did I already tell you I like it! If you would not mind, I would like to add your link to my "favorites" page to come back and read again sometime. Should you ever need it, there's lots of information on this site about small business web hosting services. Again, great blog and keep up the great work!

     
  • At 7:15 PM, Anonymous calgary web hosting said…

    I have been on-line searching for hours for information regarding best web hosting company and stumbled across your blog during my journey :-) Niks your blog is really amazing! Keep up the great work. Obviously my search on best web hosting company was way off when compared to PHP vs CGI and find it funny how it landed me here. The internet is a funny thing. Anyways, great job on your blogging and keep up the good work! I been searching for best web hosting company for over 2 hours and needed a break from it. I started reading your blog and really started getting into it.
    P.S I will add you to my favorites so I can come back and visit later
    P.S.S If you want to bookmark my site I am at best web hosting company. You never know you may find some good deals!

     
  • At 6:21 AM, Anonymous web hosting company for small business said…

    What up Niks! I just finished up a ten hour work day and decided to kick back and do some surfing. So I grabbed myself a drink and stumbled across your blog while doing some research on calgary web hosting for a upcoming project I am doing. Well even though PHP vs CGI isn’t what I was looking for I really enjoyed reading your blog. Your doing a great job and please keep up the good work. Lots of people do not keep their blogs up to date :0) There are some very interesting view points stated here. Anyways I am going to grab the bull by the horns and continue to plug away at calgary web hosting. I have already bookmarked your blog. You many want to visit me at calgary web hosting. You never know you might see something you like! Again great job.

     
  • At 6:02 PM, Anonymous lowcost web hosting said…

    Hey Niks. Very nice blog :0) I just got inside from washing and waxing my truck. It is my baby. Took me 2 hours though. So I settled down into my basement and started doing some web surfing. Anyways I am in the process of grabbing my masters degree and have spent the last 6 months researching commercial web hosting. In the midst of my surfing I landed smack dab in the middle of your blog. I hope you do not think I am intruding but I must say it is great blog. Even though PHP vs CGI is way off base from commercial web hosting I found myself cruising through your blog archives for the last half hour :0) You have some nice blogging friends. Anyways, I need to get back to my mission. I wrote don’t your url and feel free to visit me here at commercial web hosting. I am so busy so I can only update my site monthly. Keep up the great work.

     
  • At 6:26 PM, Anonymous calgary web hosting said…

    Well I just got back from the gym and I am beat. I am currently doing some research on calgary web hosting and stumbled across your blog. Which cracks me up really. The internet can certainly land you off base sometimes. Even though PHP vs CGI is not completely related I think it is a cool blog. I have read back through the archives and lots of people make some very good points. Well I have been on-line forever it seems. I need to continue to plug away at calgary web hosting. If you have the energy swing by calgary web hosting. I try to update my site weekly and maybe you will see something you like. I already snagged your URL and put it in my favorites. If you do not mind I will be back again. Great job!

     
  • At 7:28 PM, Anonymous web space hosting said…

    Hey Niks. Very nice blog :0) I just got inside from washing and waxing my truck. It is my baby. Took me 2 hours though. So I settled down into my basement and started doing some web surfing. Anyways I am in the process of grabbing my masters degree and have spent the last 6 months researching personal web site hosting. In the midst of my surfing I landed smack dab in the middle of your blog. I hope you do not think I am intruding but I must say it is great blog. Even though PHP vs CGI is way off base from personal web site hosting I found myself cruising through your blog archives for the last half hour :0) You have some nice blogging friends. Anyways, I need to get back to my mission. I wrote don’t your url and feel free to visit me here at personal web site hosting. I am so busy so I can only update my site monthly. Keep up the great work.

     
  • At 1:18 AM, Anonymous calgary web hosting said…

    Well I just got back from the gym and I am beat. I am currently doing some research on commercial web hosting and stumbled across your blog. Which cracks me up really. The internet can certainly land you off base sometimes. Even though PHP vs CGI is not completely related I think it is a cool blog. I have read back through the archives and lots of people make some very good points. Well I have been on-line forever it seems. I need to continue to plug away at commercial web hosting. If you have the energy swing by commercial web hosting. I try to update my site weekly and maybe you will see something you like. I already snagged your URL and put it in my favorites. If you do not mind I will be back again. Great job!

     
  • At 7:49 PM, Anonymous net web hosting said…

    I love your blog Niks. How long has it been on-line? Reason I ask is I am doing a ton of work in the area of small business web hosting services and will probably end up starting a blog of my own. Funny how the internet brought me here when I was doing searches on small business web hosting services. Oh well, I am glad it did. Keep up the great blogging and I am sure I will visit PHP vs CGI again!!

     
  • At 5:38 AM, Anonymous personal web site hosting said…

    Well this blog certainly is not about web hosting company for small business. What the heck! I guess the internet can play some tricks on us sometimes. I have been on-line for two hours
    researching web hosting company for small business and came tumbling across your blog. I LOVE IT! I needed a break from web hosting company for small business anyways :-) If you don't mind I want to add your
    blog to my favorites list so I can come back later on and read some more stuff. Well I guess I should get back to researching web hosting company for small business.
    Even though my search is not on PHP vs CGI I am glad I came across your blog. Keep blogging away!

     

Post a Comment

<< Home