PHP or Perl? A quick look for beginners
Matt on 2001 April 16
    

I've been playing around with php, and I like it a lot more than Perl. It's always a drag to learn something new when you know the old way well enough - even if the old way is less efficient.

But php is so much better then Perl because php was written for the web, whereas Perl was adapted to it, and thus isn't quite as efficient to both write for and execute on the web.

This article is directed towards the user looking to learn a backend scripting language or the user just learning Perl. (I rate myself as an intermediate Perl/CGI scripter and a php newbie)

.



The best example of php's superiorty over Perl is the way php handles form variables.

I'm going to use an example, the LinksMenu with a Flash interface that I made. (View it at the top of the page here and download it here.) In Perl, you have to process all the incoming variables with something like this:



#!usr/bin/perl
use CGI;
$foo=new CGI;
print "Content-type: text/htmlnn";
$y=1;

while ($foo->param("url$y") ne ""){

$url[$y]=$foo->param("url$y");
$desc[$y]=$foo->param("desc$y");
$sitename[$y]=$foo->param("sitename$y");
$y=$y+1;

}



In php, none of that slop has to take place because php automatically catches and assigns any incoming variables.

For instance, in php, the above looks like this:


<?php


?>


That's right, that's an empty line you're looking at.

You don't have to do anything because php 'transparently' does it all. Thus, any variable from the form you send declared as 'name=joeuser' automatically gets set up in php as '$name=joeuser'. One of the benefits of php being written FOR the web and not ADAPTED to the web. There are other advantages pertaining to using php with forms, but that's a whole other article.



I didn't even get into using MySQL with Perl because it seemed too overwhelming, (just what is the Perl DBI?) and I just stuck with flat files for data storage.

Learning php with MySQL is a lot easier and MySQL is much faster and more efficient than simple flat files.

The first and most apparent drawback to using flat files as a database is that you have to read all the data from the file in, make whatever changes need to be made, then write all the data back to the flat file. You have to take all kinds of precautions to make sure data isn't being overwritten by multiple users. Since all the data is being taken out and then written back in, two users could easily overwrite each other.

This same problem can exist with php and flat files, however, it doesn't exist with MySQL. Making the step up to MySQL solves the overwrite problem.

With MySQL, you can take someone's data, say their high score, and have it inserted into the database without worrying about any overwriting. (You don't have to read in all the high scores, add the new high score, then write it all back to the file.) With php and MySQL, it's pretty easy to do. It might seem a little daunting at first to figure out how to connect to the database and what the SQL commands mean, but it will be worth it.

I recommend the book PHP Essentials. It's a simple and straightforward and will walk you through the basics. You'll be hooked up to a MySQL database in after a few lessons. There are sections on creating an e-commerce site with a shopping cart, how to set and use cookies and few other tid bits. And most importantly, the author is still supporting the book on her website with corrections and additional tutorials.

I'm also using php4 Bible, but I'm not as happy with it. Being a 'Bible', they try and fit as much in as they can, but they don't do justice to all of their lessons as a result. (And granted, I'm not the fastest learner). Also, their support page has been 'under construction' for some time now, without any corrections for their book being included.

Thanks for reading!

Gotta say, whoever wrote this, please, let me know who you are so I can give you credit. =) Thanks for the article though.
Tentatively planning to Open Soon! (no dates ...) // Doing heavy development now...