Skip to content

Instantly share code, notes, and snippets.

@jerclarke
Created March 30, 2016 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerclarke/81f06503ca5f09bb0895332a54e1b54c to your computer and use it in GitHub Desktop.
Save jerclarke/81f06503ca5f09bb0895332a54e1b54c to your computer and use it in GitHub Desktop.
Simple PHP script to test display of errors including a fix for displaying inline errors in HHVM
<?php
/*
* Test display of errors in HHVM/PHP
*
* In HHVM and often in normal PHP I want to see notices and errors inline to the
* document. This script tests the ability to make errors visible and has a solution
* that so far works in all the modern PHP and HHVM versions I tried.
*/
echo "<h1>Error test document for PHP</h1>";
/*
* SOLUTION: use set_error_handler() to explicitly define our expected behavior
*/
echo "<h2> SOLUTION FOR HHVM: use set_error_handler() to explicitly define our expected behavior</h2>";
echo "<code>set_error_handler(function ($errorNumber, $message, $errfile, $errline) {
switch ($errorNumber) {
case E_ERROR :
$errorLevel = 'Error';
break;
case E_WARNING :
$errorLevel = 'Warning';
break;
case E_NOTICE :
$errorLevel = 'Notice';
break;
default :
$errorLevel = 'Undefined';
}
echo '<br/><b>' . $errorLevel . '</b>: ' . $message . ' in <b>'.$errfile . '</b> on line <b>' . $errline . '</b><br/>';
});</code>";
set_error_handler(function ($errorNumber, $message, $errfile, $errline) {
switch ($errorNumber) {
case E_ERROR :
$errorLevel = 'Error';
break;
case E_WARNING :
$errorLevel = 'Warning';
break;
case E_NOTICE :
$errorLevel = 'Notice';
break;
default :
$errorLevel = 'Undefined';
}
echo '<br/><b>' . $errorLevel . '</b>: ' . $message . ' in <b>'.$errfile . '</b> on line <b>' . $errline . '</b><br/>';
});
error_reporting(E_ALL);
ini_set("display_errors", "On");
echo "<h2>Test a notice-generating error</h2>";
echo "<code>echo \$undefined_variable;</code>";
echo "<code style='margin:1em;padding:1em;background:#ddd;display:block;'>";
echo $undefined_variable;
echo "</code>";
echo "<h2>Test a fatal error</h2>";
echo "<code>\$not_a_class = '';
\$not_a_class->not_a_method();</code>";
echo "<code style='margin:1em;padding:1em;background:#ddd;display:block;'>";
$not_a_class = '';
$not_a_class->not_a_method();
echo "</code>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment