While browsing the shelves at the local bookstore I made a mental note: There aren’t many PHP books. I picked up a few and took a look inside. They were ok, but, right off the bat, I felt like they were teaching the readers bad habits. I think that the most important time for a programmer, or a newbie programmer, to form good habits is when he writes his first few lines of code. I thought I’d take a few minutes and put down some of my pet peeves and see what others thought.

PHP is not just a “scripting language.”

Let’s get that out of the way. PHP is a full blown Web Application Development language. It can do anything a skilled programmer wants it to do. It benefits from being easy to read, easy to understand, and has roots in C, one of the grandaddies of programming languages. Although syntacticly similar, PHP is not C and doesn’t need to be. The point is, once you learn PHP, you can more easily grasp other languages such as Java, and C, etc.

Separate your laundry, please

Although you can mix PHP in HTML, SQL, etc, it doesn’t mean you should. Try to treat each layer as a separate realm. It’s ok to use PHP in HTML for presentation purposes, such as displaying the date, etc. But you should not place your business rules, database queries, etc, in your HTML files. It leads to horrible problems and bad habits. Yes, I concede, it still produces a working program, but you will thank yourself later if you put some discipline and structure into your coding style early on.

Create functions for database queries

This is a very important lesson. Do not be tempted to write out a quick and dirty mysql_query statement in the middle of your page. You will find yourself writing the same queries over and over. As soon as you change the database structure, you have many pages to fix. Write a function library for all of your queries, this leaves you with one file to modify, and, gives you the bonus of writing reusable functions.

Find a framework, learn the framework, the framework is your friend

There are countless “application frameworks” that help you create programs faster, better, smarter. Zend Framework, CakePHP, Drupal just to name a few. Learn how they solve common problems. Writing code is usually writing the same code over and over. Best to use someone else’s framework because they have already addressed many of the problems you will face.

PHP is a powerful programming language. With it you can produce great software, connect it to popular database engines like Mysql and Postgresql, and it has the added benefit of being installed on almost every webserver on the planet. PHP won’t let you down if you give it enough time and patience to learn PHP programming the right way. There are thousands of top notch software in use today with PHP that were written the wrong way, but they still function. It’s just a major challenge in maintaining the spaghetti code. Make your life easier with PHP, learn the right way, early on.

This is just a few ideas to get the new PHP developer on his feet. I’m sure there are many more suggestions, can you think of any off the top of your head?