A handful of best practices that are important for PHP developers to follow

A common misconception about professional programmers is that they always obey common sense, best practices and handling their coding. This is no truer than a professional chef always following the strict approach talk to them in culinary school, or a celebrated writer obeying the rigid rules of English when formulating their particular writing style.

In other words, it generally doesn’t happen. The biggest problem with this, honestly, is that these are the same developers that provide examples for newcomers, and this results in these novice programmers picking up bad habits, and assuming by example that they can ignore best practices because the pros do. In an effort to nip that in the bud, let’s take a look at some best practices for implementing PHP. Professional coders, this applies to you just as much as a newcomer to the world of computer programming!

Commenting and Documenting

It’s a shame that professional coders need to be reminded of just how important properly documenting and commenting their code can be. When it comes time to reuse code, update code, or for a new developer to take over a project, a lack of comments can be a disaster. Just because a developer has a strong understanding of the language does not mean that it’s easy for them to easily read code written by another developer. They can do this, but they’ll spend a lot of time coming to terms with the previous developers work before they can get anything constructive done.

When the previous developer fully comments the code, explaining what large blocks of it are for, and providing clear explanations for what different lines of code do, a newcomer to the project can easily come to grips with it and get moving. Along with this, it makes it a lot easier to troubleshoot when something breaks in the code, or when updates need to be done a long time after the initial development.

Coding Standards and Consistency

Another crucial aspect is consistency and a clear standard when implementing the code. This applies to the scheme through which variables are named, functions are named etc., as well as line lengths, whitespace, and so forth.

Maintaining this consistency makes the code more readable, and can actually make it faster in some cases as well.

Treating Strings Nicely

Strings are already a taxing type of data to operate on. As a matter of fact, as far as the universal basic primitives most languages support go, strings are the slowest all around. This means that properly formatting strings, minimizing the operations and comparisons done on them, and sticking to a scheme a single or double quotes will drastically improve the efficiency of your coding, especially with a JIT framework like PHP.

Efficiency

Staying with the theme of treating the PHP VM nicely, it’s important to code efficiently. This means avoiding the use of function calls inside of loops, avoiding extremely deeply nested if, switch or loop statements, and other such convolution. Jumping around more is often avoided in other programming languages, but remember, all enclosures inside a language like this have to be digested and large chunks. This means a slower response and an increasing lack of accuracy.

Arrays

Arrays can be treated in one of two ways in PHP – with the index being inside or outside of quotes. There is a distinct difference between the two, which the PHP manual very clearly illustrates. Understand these differences, and use the appropriate approach. If you don’t, you will spend a long time trying to figure out why the results of your code aren’t matching your expectations.

Use Your Tools

IDE’s and frameworks are your friends. The archaic concept of bragging that you simply use Notepad or a similar simple text editor to write your code is absurd. With an IDE, you have a sophisticated syntax highlighting system that makes the code more readable, integrated error-checking, and tools to ensure that you’re complying with W3C standards.

Similarly, frameworks can save you a lot of time reinventing the wheel. If a solution has already been developed, and is packaged for widespread use in multiple projects, there’s no sense in reinventing this yourself.

Also in this vein, never forgets to enable error checking in your local instance of PHP. You can spot a lot of problems, efficiency issues and so on that you otherwise wouldn’t notice. Remember, just because a bug isn’t causing the code cannot execute, doesn’t mean something isn’t wrong.

Don’t Repeat Yourself

Use functions. Optimize your code so that a frequently used piece of it is easily called from anywhere else, in the form of a function. There is no sense in rewriting the same pieces of code over and over again, ballooning the size of the file and stressing out the JIT even more.

Long Tags

Most of the above best practices apply to a lot of languages, at least conceptually. However, this is a PHP specific issue, and it’s a horrible habit to pick up. The use of short tags such as [? to define the presence of PHP does indeed work, but just because this works doesn’t mean you should do it. Always use the standard along tag definition to layout the presence of PHP in a document. This makes it stand out both for developers and designers. It also helps the browser cache things better in some cases.

To learn more about PHP, and to see even more excellent best practices for being the best, most appropriate and efficient programmer possible, fill out our contact form below. We are eager to help you be the best PHP developer you can be.