PHP Debugging

PHP Debugging

PHP Debugging and Script Efficiency

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”.

<?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);

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.