Tuesday, 08 October, 2002

Learning PHP

My latest educational experience:  learning PHP—a Web scripting language widely used on Linux.   I'm experimenting with some Web stuff on my Linux From Scratch system and, since there's obviously no VBScript available (fortunately), I'm forced to use something else.  Given the choice between Perl (which I've played with briefly) and learning something new, I chose to try my hand at PHP.  I picked up Sam's "Learn PHP in 24 Hours", (which isn't a bad book, although it's not very good) and started going through the tutorials.  The first 8 chapters introduce basic language concepts, reinforcing the explanations with little examples that you can run on your web server.  The remainder of the chapters show you how to use the language for many common tasks.  The descriptions in the first 8 chapters are adequate.  As I'm currently on Chapter 9, I can't say anything about the rest of the book.

The best way I can describe PHP is "C for the Web."  The language looks very much like C would look if you took away basic data types (all variables are untyped) and then added a few object-oriented features.  When I first saw the language, it looked reasonable.  But digging in, I'm not so sure.  The language has evolved rather than having been designed, and it shows.  There's some serious wonkiness there.  For example:

I can understand that PHP is intended as an interpreted scripting language rather than a compiled language.  It's common for scripting languages not to require pre-declaration of variables (although an option would be nice).  But not printing an error when you try to reference an undefined variable is weird.  If I write print ("$x"); when x hasn't been defined, PHP just prints nothing.  (Update 10/13 - my PHP configuration prints a warning.)  I would have expected it to print an error message instead.  I just know that referencing undefined variables is a huge source of bugs in PHP programs.

I won't even get into the strange array semantics, or the odd object model.  Both are usable, I think, but very weird.  I'm still learning about the language, and I likely will write some small web applications with it, but I'm also looking for something a bit less odd.  Unfortunately, the only real options I see are Python, Perl, and Java.  The other two P's look to be stranger than PHP, and Java, well...  Is there really no decent web programming language?  What I wouldn't give for Web Pascal.