PHP Debugging: Displaying Error Message in HTML output

When debugging PHP code, it can be useful to display PHP errors within the output. Normally, PHP error messages are written to the Apache log.

To have PHP errors in your browser output, you can use this command in your PHP script

ini_set(‘display_errors’, 1);

Below is a short example PHP script that demonstrates.

<?php
print “The following include fails quietly.<br/>n”;
include(“first_missing.inc”);
print “After first failed include.<br/>n”;
print “——<br/>n”;
print¬† “Now we switch on display_errors.<br/>n”;
ini_set(‘display_errors’, 1);
print “——<br/>n”;
print “Now, the second include fails with following error messages.<br/>n”;
print “<br/><i>n”;
include(“second_missing.inc”);
print “</i><br/><br/><br/>n”;
print “After second failed include.<br/>n”;
?>

And here is the output from the script.

The following include fails quietly.
After first failed include.
——
Now we switch on display_errors.
——
Now, the second include fails with following error messages.

Warning: include(): Unable to access second_missing.inc in /export/www/sites/testsite/error3.php on line 19¬† Warning: include(second_missing.inc): failed to open stream: No such file or directory in /export/www/sites/testsite/error3.php on line 19 Warning: include(): Failed opening ‘second_missing.inc’ for inclusion (include_path=’.:/usr/share/php:/usr/share/pear’) in /export/www/sites/testsite/error3.php on line 19

After second failed include.


This entry was posted in linux.