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.
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.
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.
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.
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.
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:
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!