How to Produce Good and Resource-friendly PHP Code

Looking at Essential PHP Development Practices

PHP is the pop web-development language of the day, easy to learn and apply, with thousands of free resources available all across the web. However, the ease with which the basics of the language are picked up is a recipe for many coding disasters. What are good coding practices, and
 where are the common pitfalls?

This article outlines some of the facets of good and resource-friendly coding in PHP. The following, drawing from my six years with PHP and interactions with fellow coders, should also be helpful to developers who work with other languages, and even make general sense to the non-developer audience who may be looking at hiring a coder.

Does raw performance translate into goodness in coding? Well, resource-friendly coding is one thing. While it certainly is an important aspect of good coding, it alone does not good coding make. We'll move ahead and cover both areas in brief. First, let's get the general goodness covered.

On good coding practice

Think over the structure of the application and the elements required before you start coding to ensure the effective use of your time and code. Even, make dummy files for each element before you start filling them in.

Code with security in mind. In applications that take user input, which is most of the lot, ensure that the input cannot be used to exploit the code in unintended ways. Go over your own script and try to exploit it yourself.

Wherever possible, create replicable patterns instead of hard-coding everything. Use functions for recurring processing events, use arrays and loops for output with recurring patterns. That'll make the changing and the extending of the code much more pleasant an experience in the future. Even, go object-oriented and use classes.

In general, think of the code as something you need to possibly extend in the future, and keep the ends open so as to not have to re-code aspects from a scratch to accommodate future features.

Related information