Guidelines for PHP Development


This report examines the best practices that are commonly adopted in the PHP world.

Error reporting must be turned on.

Error reporting is a very helpful feature in PHP and should be enabled during the development stage. This helps us to identify issues within our code. One of the most popular functions is ‘E_ALL’, which helps us to identify critical problems and all warnings.

Indentation and use of whitespace.

When writing code in any programming language, it is important to ensure that adequate whitespace is used and that the code is correctly indented. This improves the readability of the code and helps us to maintain the code in a more efficient manner.

Use the DRY approach.

DRY stands for ‘Don’t Repeat Yourself’. This concept is a very useful development idea and can be applied in any programming language, such as PHP or Java, C#. By using the DRY approach, we ensure that there is no unnecessary code. A piece of code that violates DRY is called a WET solution. WET stands for ‘Write Everything Twice’ or ‘We Enjoy Typing’.

We have two main methods to ensure that we implement this:

  • Using Camel Case – In this method, the first word is in lowercase and the first letter of each subsequent word is capitalized.
  • Using underscores between two words – In this method, we place an underscore character between every two words.

Deep nesting should be avoided. Any developer should avoid deep nesting.

Use adequate comments.

This is standard practice and should be implemented. This helps because it is usually the case that the person who is writing the code is not the same person who will be reviewing the code later. Inline comments will be helpful in understanding the purpose of writing the code even if the same person is asked to make some changes in the code. To maintain high-quality comment standards in PHP, I recommend familiarizing yourself with a PHP documentation package such as phpDocumentor.

phpInfo() is an essential function and should be used with utmost care. By using this function, anyone can obtain the details of the server environment. It is always recommended to keep the file containing the phpInfo() function in a secure area. It should be removed from the code once development is completed.

Never trust the user.

Create your code in such a way that it can handle a variety of possible inputs if your program requires any user input. A good strategy to protect your software from hackers is to initialize your parameters with some initial value that will be irrelevant in the current business flow.

Use a cache system wherever required.

Good programming practices always recommend using the cache system as caching helps us to achieve better performance. In the PHP world, caching is achieved using:

  • XCache – a fast reliable PHP opcode cache
  • Zend Cache – a collection of APIs for realizing advanced caching capabilities.

Avoid copying extra variables.

It is not a good development practice to copy predefined variables into local variables with smaller names. This has a negative impact on the performance of the application.

Use frameworks.

Frameworks are developed after a lot of research and therefore they prove to be beneficial. In PHP, there are plenty of frameworks available. During development, you should take advantage of these. One of the popular frameworks is Model View Controller or MVC.

PHP video tutorials are also available to help with learning.