Right from its inception, PHP was widely used to develop web based applications. Since PHP is a scripting language, one must follow some rules while developing.


This article will discuss the best practices that are generally followed in the PHP world.


1. Error reporting should be turned on

Error reporting is a very useful function in PHP and should be enabled while in the development phase. This helps us to identify the problems in our code. The most commonly used feature is 'E_ALL', which helps us to spot all the warnings and critical errors. It must be noted that before we put our code into production, we should turn off this feature as this would expose all the potential errors on the browser.

2. Use the DRY approach

DRY stands for Do not Repeat Yourself'. This concept is a very useful programming concept and should be used in any programming language, such as Java, C# or PHP. Using the DRY approach we ensure that there is no redundant code. A piece of code that violates DRY is referred to as WET solution. WET stands for write everything twic or we enjoy typing. Let us have a look into the following code:


Listing 1: DRY and WET approaches

3. Indentation and Use of whitespace

While writing code in any programming language, you must ensure that the code is properly indented and sufficient white space is provided wherever required. This increases the readability of the code and helps us to maintain the code in a more efficient manner.

4. Meaningful and consistent naming standards

As in any programming language, PHP experts also advise to follow meaningful naming standards. We have two major approaches while we ensure to implement this:


      • Using Camel Case– In this approach, the first letter is in lower case and first letter of every word thereafter is in upper case.


Listing 2: Code snippet using camel case

  • Using underscores between two words– In this approach, we put an underscore character ('_') between every two words. Using this approach, the code can be modified as follows:


Listing 3: Code snippet using underscores


5. Deep Nesting should be avoided

Multilevel nesting reduces the readability of the code regardless of programming language. Any programmer should avoid using deep nesting.


Listing 4: Code snippet having multi level nesting

6. Use adequate comments

As in any programming language, make sure that your source code has sufficient inline comments. This is a standard practice and should be followed. This helps in further analyzing the code base as it is a often the case that the person who is developing the code is not maintaining the same. Even if the same person is asked to make some changes in the code, inline comments will always be helpful to understand the motive of writing the code. In order to maintain high class comment standard in PHP I would recommend you familiarize yourself with a PHP documentation package such as phpDocumentor.


7. Do not put phpInfo() function in web root

phpInfo() is a very important function and should be used with utmost care. Using this function, anyone can get the details of the server environment. It is always advisable to keep the file containing phpInfo()function in a secured location. Once the development is done, it should be taken out of the code immediately.

8. Never trust the user

If your application involves any user input, write your code in such a way that it can handle all sorts of possible inputs. A good approach to protect your application from hackers is to always initialize your variables with some initial value that may not be relevant in the existing business flow.

9. Use Cache mechanism wherever required

Good programming approaches always suggest using the cache mechanism as the cache helps us to achieve better performance. In the PHP world, caching is achieved using:

10. Avoid copying extra variables

It is not a good programming practice to copy predefined variables into local variables having smaller names. This has an adverse effect on the performance of the application. Let us see the following code snippet:

11. Use frameworks

Frameworks are developed after a great deal of research and hence they prove to be less problematic. They make our lives easier as they provide proven solutions. In PHP there are lots of frameworks available. During development, you should make use of these. One of these frameworks that are widely used is MVC or Model View Controller.