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:

  • When passing parameters to functions, you enclose the parameters in parentheses.  Except for the print function.  It’s special, and you don’t have to parenthesize its arguments.  Weird.  Special cases make a language more difficult to use.
  • All variable names begin with a dollar-sign ‘$’ character.  I know this is popular in some languages, but it’s always struck me as some kind of weird syntactic sugar.  Function names and constants don’t need special identifying characters.  What’s so special about variables that they need to be flagged?
  • The operators && and and do the same thing:  they perform a logical and of the two operands.  The only difference is that && has higher precedence than and.  Why?  It makes absolutely no sense to have two different logical and operators, and giving them different precedence is just wrong.  The operators || and or (logical or) have the same weird relationship.  The problem of precedence is much more easily solved by using parenthesis to specify evaluation order.
  • The language defines an “else-if” construct that I wish was in every language.  In PHP, the “else-if” can be written as a single word, elseif, or as two words, else if.  Why clutter up the syntax like that?  It makes the language harder to parse, and provides no real benefit.  Throw out the two-word else if, and save yourself some headaches.

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.