A Few Words About Debugging – Part I

PHP is an interpreted language, not a compiled one, which means that all errors occur when a script is being loaded or run. The errors that are easiest to fix are the syntax ones, such as missing semicolons or unclosed brackets, and the most difficult ones are the logical errors, such as a reference to a nonexistent object.

Anyway, when something goes wrong, your first aid kit is a good PHP configuration and a set of handy tools/extensions.

1) Configuration

You may not always have a development server, and some settings depend on the server type (development or production one), thus: below we describe three types of configuration: general, production and development.

General configuration

For each server, be it a production or a development one, you should enable error logs, so that you know which errors the users encounter. Additionally, it is best to have the errors displayed as HTML. This will make the logs more legible.

1 2 3 - log_errors = On # Enables logging errors to a file - error_log = php_errors.log # The path to the log file - html_errors = On # Enables HTML formatting of the errors

Production configuration

A production server is the one with live websites – the ones that are accessible to the end users. This type of a server cannot display errors and warnings in the browser because they can include information that would make the site vulnerable and could confuse the users.

1 2 3 4 - display_errors = Off # Disables displaying errors in the browser - display_startup_errors = Off # Disables displaying PHP boot errors in the browser - track_errors = Off # Disables storing the last error/warning in the $php_errormsg variable - error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

Development configuration

A development server is where the website is actually developed. This server type can be accessed only by selected users, such as developers, QAs, etc. The database in this installation should not store the real user data, such as clients' orders or other private information. Moreover, the development configuration might not be stable due to the changes made in the development process. Therefore, it is important to have each error or warning displayed in the browser and in the log, as it makes coding much easier.

1 2 3 4 - display_errors = On # Enables displaying errors in the browser - display_startup_errors = On # Enables displaying PHP boot errors in the browser - track_errors = On # Enables storing the last error/warning in the $php_errormsg variable - error_reporting = E_ALL # Enables reporting all errors

What can you do when you do not have a development server?

In this case you should copy the website's installation to another subfolder, place the “.user.ini” file there with development PHP directives, duplicate the database, clear the irrelevant/private data and connect the database with the development installation.

You can easily check the current server configuration by placing “info.php” file next to the “index.php” file with the following content:

1 <?php phpinfo(); ?>

Now you just need to open “http://example.com/info.php” in your browser to see the current PHP settings. Remember to remove the “info.php” file afterwards.

All development work should be carried out only on the development sites. Use the production site only as a last resort.

Stay tuned for Part II with some useful debugging tools!