2006: Learning PHP 5

Book review: Learning PHP 5

This is a well written book and a good introduction to PHP. If you're an experienced programming, many of the explanations are unecessary, but it doesn't take long to scan through them. What I really appreciate is this book's length: 295 pages if you read the first two appendices. That's impressive. Short is good. The author accomplishes this, in part, but offering teasers for certain language features that communicate the jist of what can be done and how to do it without going into unecessary detail.

The efficiency with which you can read this book is fantastic. If every computer book were this straight forward, you could read 10 a year and add countless value to your toolset.

Language review

PHP is a great choice for developing web applications. My main point of reference is Perl, and it is clear to me that PHP is better suited for the task.

Notable features:

The PEAR DB facility is impressive. It allows you to connect to and use nearly any database in an efficient, straight forward manor. Probably the best database interface I've ever seen.

For starters, check out this connect statement:
$db = DB::connect( 'mysql://username:password@server/databasename' );

This DSN syntax is very compact and intuitive. I love it!

It's the way that you interact with query results that is more important in the long run, however. PHP's object interface is also brilliant:

$query->field

This gets the value from the field 'field'. Could it be any easier?

The most interesting and probably one of the most convenient and powerful features of PHP is the strtotime function:

$mydate = strtotime( 'next thursday' );
$mydate = strtotime( '+4 days', $mydate );

This kind of date recognition reminds me of a date field that I programmed into an application a few years ago that accepted a wide range of date formats. If you ask me, computers can and should be able to accomodate this type of functionality and PHP makes it happen. Bravo!

PHP's implementation of sessions is easy to use and brings a lot of power to the table

PHP allows you to use URLs in place of file names:

$data = file_get_contents( 'http://www.danielbigham.ca/' )

PHP is an XML whiz:

$xml = simplexml_load_file( 'http://www.danielbigham.ca/rss.xml' );
print "The first item in the RSS feed is '$xml->channel->item[0]->title'\n";

It doesn't get much easier than that. (Well ok, the keyword simplexml_load_file is cumbersome, but other than that this is pretty close to ideal)

As we all know, the web is susceptible to attackers. PHP offers convenient functions for escaping the values you get from web users:

The quoteSmart and strtr functions allow wilcards and apostrophies to be escaped to prevent your database from being compromised

The htmlentities function encodes HTML's special characters to prevent users from having their HTML or JavaScript appear on your site

Where PHP is lackluster:

PHP's naming conventions are decent but not good

PHP is a function-oriented language. Most verbs are functions, not methods. This means that the namespace is pretty flat -- ultimately, that functions have somewhat obscure names. (See above point)

For such a web-oriented language, I'm moderately dissapointed in its lack of build-in HTML-generation facilities. True you can use the HTML_QuickForm package, but I would have hoped that the authors of PHP would have deeply integrated this type of thing into the language. For example, ASP.NET introduces some pretty novel ways of programming web forms.

With the little bit of experience I have actually writing PHP code, it quickly became apparent that PHP compile errors leave much to be desired. I don't get it! Why do language designers not put more effort into making clearer error messages?

Again, I don't have much experience in this area, but I'm not aware of a PHP debugger. I don't remember the book mentioning one. If this is the case, it is a major drawback to the language. Shouldn't every language have a powerful debugger?

PHP is not a typed language. While this is good and fine in some cases, it is a significant detractor in many others.

PHP can't be compiled