Debugging PHP Scripts

Probably the most frustrating part of the development process is debugging. Even the most experienced programmers take their time to debug and improve their applications; if you're a beginner, this should be an essential step in your learning. Remember that a computer application doesn't do what you want it to do, it does what you tell it to do.

PHP's error messages always give you more or less information on what's wrong with your script. But first thing you need to do is check your configuration file. You can do this using the "phpinfo()" function, which outputs a lot of information in friendly-reading style, including the configuration in "php.ini". Give it a try:

<?php phpinfo() ?>

A very important setting in PHP's configuration is "display_errors". If you disable this setting, you will not see any errors from PHP, so if your script isn't working from various reasons you will just see what has been outputted so far, and that's it. Enabling this setting will make PHP output error messages, according to another setting called "error_reporting". You can choose to have displayed fatal errors, warnings (non-fatal errors), notices (potential problems), user errors, etc. These settings only report the problem to the browser, they have absolutely no effect on the action taken when an error is found. You can also do this from a script, using a function called "error_reporting()":

error_reporting(E_ERROR); //displays only the errors, no warnings or notices

While this type of debugging is suitable for development purposes, you will have to handle bugs or errors in public web-sites. Simple actions like renaming a file or directory can cause major problems if you don't take into consideration all of the web-site's structure. For these unfortunate cases, PHP provides a built-in function which is used to log errors to the server's error log file or another file of your choosing: "error_log()". This function requires as arguments the error message itself and an integer representing the message type, saying where the message should go (the system logger, email, remote connection or a file). If you want to write an error message to an error log, a destination must be set for the "error_log" option in "php.ini".

Sometimes you can define the error string yourself, but what about when an error is caught by PHP? To read the error message, you must first set the "track_errors" in your PHP configuration file to "On", then you can access the most recent error in the variable "$php_errormsg":

fopen( "important_file.txt", "a" ) or

error_log(__FILE__ . " line " . __LINE__ . ": $php_errormsg", 0);

There isn't really a technique for minimizing errors and bugs, all you can do is stay vigilant and try not to do multiple things at the same time. Don't write functions that do a lot of things at the same time, and try to separate the pieces in your code, so you will be able to better isolate and repair an error. Also, try to test your application as often as possible. Write one or two functions, and then see if they work properly. It's one thing to track an error from within a dozen functions, and another thing to find an error in the last function you wrote. Always be prepared for the worst, and try to consider all the possible aspects, not just the ones of the intended action. For example, if you need to write something to a file, always test if the file exists, and weather you have the appropriate permissions to write to the file.

blog comments powered by Disqus