Tag Archive for 'debug'

How to enable error reporting in PHP script

If error reporting is disabled by default on your host you have two solutions.
First is to add to your PHP script this little piece of code:

<!--?php 
  error_reporting(E_ALL);
  ini_set("display_errors", 1);
  // the rest of your script...
?-->

Additionally it is very useful to enable E_STRICT reporting level as well (not included in E_ALL). E_STRICT run-time notices, enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. To turn it on your code should look like this:

<!--?php 
  error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1);
  // the rest of your script...
?-->

But putting this code on the top of your script will not catch any parse errors. A missing ")" or ";" will still lead to a blank page. This is because the entire script is parsed before any of it is executed. If you are unable to change php.ini (that would be the best solution) and set in it

display_errors On

then there is a possible workaround (found on comments on php.net website). This code would solve this problem:

<!--?php 
  error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1);
  include("file_with_errors.php");
?-->

You may go even further and make a little debug script with error reporting (when it is disabled for the rest of your scripts). Just create a file called "debug.php" with this content:

<!--?php 
  error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1);
  include($_GET["file"]);
?-->

Then you will be able to execute any script on your web host just by calling "debug.php" with urls like this
http://domain.com/folder/debug.php?file=file_with_errors.php
http://domain.com/folder/debug.php?file=subfolder/file_with_errors.php
http://domain.com/folder/debug.php?file=../file_with_errors.php

Second alternative soulution is to edit your .htaccess file. It would enable error reporting for all files in current directory. All you need to do is to simply add the following lines:

php_value display_errors 1
php_value display_startup_errors 1