PHP is maturing as a language as well as a technology for building a wide range of applications. PHP 7 is coming out this year and it’s more powerful and faster than it ever was. There’s a lot to look forward to, but there’s still plenty of room for improvement for PHP and for us as programmers.

Part of the reason for the PHP hate circlejerk is often explained as being fueled by its inconsistencies and functional shortcomings. This is definitely true for the older versions, but less and less relevant with every new release. The decisions and performance of the internals developers are another important contributing factor. The short array syntax, as an example, was proposed in 2003 and implemented only in 2012. But the subject of this post is the incompetence of programmers and programming book authors.

PHP programmers handbook

The first book on PHP I read was printed in 2003. Having a homepage was the hottest thing back then. Thanks to free shared hosting even kids could make a simple website in Microsoft FrontPage, upload it through FTP and marvel at their own creation. Flash buttons and frames weren’t enough for me, so I had to learn a scripting language. The book, examples from which are provided below, focused on PHP 4, but offered advice on comparability with PHP 3. It looked fine for a complete beginner. Revisiting it after so many years is quite entertaining.

The strings

You can use double quotes, you can use single quotes, this book uses accents to encapsulate strings. You can use grave accents (backticks) in PHP to execute commands, but you can’t encapsulate strings with them. Acute accents won’t work either. I have no idea how this made it into the book.

Tabs? Spaces? Nope.

This line alone is amazing for many reasons. No one bothered running the code, before putting it in the book.

Capitalization is inconsistent.

This code was supposed to demonstrate how operator precedence works. The authors of the book didn’t bother saving the result.

The loops

Including a function like count() or sizeof() within a loops execution condition is a bad idea. This expression will be evaluated with every iteration.

Instead you should use something like this:

$count = count($array); for ($i = 0; $i<$count; $i++) {}

or this:

for ($i = 0, $count = count($array); $i<$count; $i++) {}

Variables

The names for the variables don’t make any sense.

If they wanted to show and example for a while loop they could have named the variables $i and $max instead of $my and $name. That would make more sense.

The visitor counter

This was my favorite script in the book. It was small, simple and I needed it very much. I copied it, saved and ran it. And it didn’t work. Again no one bothered running the code before including it in the book. Can you find the problem?

Apart from the </php typo, the authors forgot a curly bracket after $counter = 0; .

SQL injection

Here the authors of the book surpassed themselves. Not only is <?php missing after the <body> tag and the $ sign is missing from submit, register_globals is on and no validation or cleaning is performed before inserting a row.

The aftermath

Awful learning materials bred bad programmers who wrote horrible software and more awful books. The momentum was strong and lasted many years. The next PHP book I read was printed in 2005 and still had code with SQL Injections, but at least no more register_globals.

Share this: Facebook

Twitter

LinkedIn

Reddit

