Best Practices in PHP

投稿日 - 最終編集日

PHP has now been around for 20 years, after the first version of the language appeared in 1995. During this long period, the language went through a lot of changes and improvements and has provided a lot of developers with valuable experience. In this article, I will describe some best practices and guidelines that can be applied in Web applications built using PHP.

Handling International and Multilingual Applications

These days, all the applications that you develop need to be able to support multiple languages. English is usually the default language, but it's a good practice to set up and prepare the application for supporting multiple languages. You'll never know when you're going to need to add support for languages like Spanish, German, or Mandarin. If your application is not prepared for this from the start, it can cost a lot of work hours to refactor the code and database to add support for extra languages. With this in mind, remember to always use UTF-8 encoding from the start. Start with the database, and make sure every text field supports the UTF-8 character set.

In the PSR1, one of the PHP coding recommendations, it is required to save all the PHP files using the UTF-8 charset. This will prevent any issues if you need to add any special character, constant, or line of text to your PHP source down the line.

To handle the strings to support UTF-8 encoding, you can use Multibyte Strings. You can check if you have multibyte string support in your PHP version by invoking the phpinfo() method. The output looks similar to the image below:  

phpinfo

When using mb_string methods, it's always a good practice to set the character set of the HTTP output and the internal encoding to UTF-8. You can do this using these two methods:

        mb_internal_encoding('UTF-8');
        mb_http_output('UTF-8');

When you are using mutibyte strings, you cannot use the normal string methods like strlen(), substr(), strtolower(), You will need to use the mb_ prefixed versions, mb_strlen(), mb_substr(), mb_strtolower.

Connecting to Databases

When building Web applications using PHP, you have a lot of options to choose from when working with databases. Most of the PHP apps use MySQL as a database engine, but others prefer PostgreSQL, SQLite, or MongoDB.

It is a good practice to use the PHP Database Object to query the database, because this supports a lot of database servers and offers a unified interface that you can work with. Additionally ,you won't have to check and verify each method of the DB driver, since the PDO does that for you. If you are using PDO you can easily change the database because of its unified interface. You can read about PDO in my other article: Using PHP PDO to manage data in MySQL.

Error Handling

When developing Web applications, we sometimes tend to forget about error handling, which is a very important part of the application from a user experience point of view. Always follow this golden rule: no Exception raised by the program should be shown directly to the user.

PHP, like any other object-oriented programming language, has exceptions. It is a good practice to create your own Exception classes. Exceptions can be technical ones, like when no connection could be made to the database or the server ran out of disk space and cannot write anything to the log files. Business logic exceptions is another type of exception; these can be like NotEnoughMoneyInTheAccountException or UserNotEntitledException, and each case should be created by you. These exceptions are raised by your PHP code and appear because there is some logical error in the workflow of your application.

It's a good thing to set the error reporting level to E_ALL using the error_reporting() method so that any error and notice will be raised as an actual error (for example a notice would be raised in case you are using an uninitialized variable or have some variable names misspelled).

Following the Guidelines

As I mentioned earlier, there are practices and guidelines like PSR1, which are created and maintained by the PHP Framework Interop Group. There are four PHP Standard Recommendations on total: PSR1, PSR2, PSR3 and PSR4. Many developers consider these guidelines very useful and they write code that follows these recommendations.

投稿済み 31 3月, 2015

Greg Bogdan

Software Engineer, Blogger, Tech Enthusiast

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript. I like technical writing and have good experience in creating tutorials and how to technical articles. I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...

次の記事

An Introduction to Creative Writing