Categories
Uncategorized

3 Essential Tips For Writing PHP Code

1. Turn All Errors On

PHP tries to give a lot of leeway when interpreting your code, continuing to run when most other languages would quit. This can be useful for rapid prototyping, but can also hide the causes of errors. To give yourself the best chance of understanding what your code is doing and where it might be going wrong it’s helpful to get as much information from the interpreter as you can, and error reporting can help with this. You can do this in your php.ini settings:

error_reporting = E_ALL
display_errors = On

or using the error_reporting and ini_set function at the top of your script:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Note that these errors should not be shown to public users of your site, so remove the above lines when your code is in production. The php.ini approach is favoured as you can have different settings on your own machine and on your public web server.

2. Write Functions

When starting a new task, it can be tempting to jump straight into writing code line by line to get to where you want to be. This can feel like the quickest way to make progress, but it will cause problems if you get lost on the way or need to undo part of your thinking. Breaking your code into functions, as well as making it easier to read for other developers, allows you to break down the problem you are tackling and give yourself smaller “waypoints” to focus on.

You don’t have to overthink these; the function names can write themselves as you think about the task from a high level. Say you had a list of user objects, each with a set of game scores, and wanted to make a league table. For each user, you would have to get a displayable name for the user and Calculate an average score. Then you would have to sort the users by this score. Finally you would have to format the output.

By breaking this work down into getDisplayName($user), getAverageScore($user_scores), and formatOutput($users_with_scores), you allow yourself to focus on each piece of work.

Note that this approach doesn’t always work forever. Breaking work into functions can prevent you making some optimisations; for instance, if you want to aggregate a list in two different ways you may need to loop over the list several times in more than one function. If this is the case, the above approach is still worth trying first to get a full understanding of your problem.

3. Find Ways To Return Early

The so called “happy path” – the path your code takes if everything is normal – can often be quite simple and clear in your mind. Many problems arise when you have to consider the edge cases of your situation and how to handle them. Item 2 in this list – Write Functions – gives a useful way to get this unpleasant work out of the way.

Take an example from above: getAverageScore($user_scores). An edge case for any average is if there are no entries to create an average from! If your code was inline you would have to create an if..else block around the entire averaging code to handle both cases. Since you’re in a function however, you can just say:

if (empty($user_scores)) {
    return 0;
}

// Everything is OK - actually do calculation below...

and continue working knowing that you actually have the data to work on.

This is technique is often called “return early” and is a great help in keeping your code straightforward.

There are many more tips and tricks to writing clear, functional code, but these three are almost always applicable. Other readers of your work – including you in a few weeks time – will be glad you followed them.

Your email will not be given to anyone else, ever.