Skip to content

Instantly share code, notes, and snippets.

@inalgnu
Created February 12, 2014 22:51
Show Gist options
  • Save inalgnu/8966181 to your computer and use it in GitHub Desktop.
Save inalgnu/8966181 to your computer and use it in GitHub Desktop.
Handle undefined index error
<?php
set_error_handler('undefinedIndexHandler');
function undefinedIndexHandler($errno, $errstr, $errfile, $errline, $errcontext)
{
if (!preg_match('/Undefined index/', $errstr)) {
return;
}
$ouput = 'Maybe you should try one of these keys : ';
foreach(array_keys($errcontext['a']) as $key) {
$ouput .= sprintf(' "%s" ', $key);
}
printf($ouput);
}
$a=['foo' => 1, 'bar' => 2, 'baz' => 3];
$a['toto'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment