An improvement to @pekka
's answer that also detects undefined array keys and offsets and undefined constants:
error_reporting(E_STRICT);
function terminate_undefineds($errno, $errstr, $errfile, $errline)
{ // $errno: E_NOTICE, etc.
$errstr = strtolower($errstr);
if (
(strstr($errstr, "undefined variable")) ||
(strstr($errstr, "undefined index")) || // Such as $GLOBALS['some_unknown_var']
(strstr($errstr, 'undefined constant')) || // Such as echo UNKNOWN_CONST
(strstr($errstr, "undefined offset"))
)
die("$errstr in $errfile line $errline");
else
return false; // Let the PHP error handler handle all the rest
}
set_error_handler("terminate_undefineds");
As above code also restricts access to unknown $_GET
and $_POST
keys, you can define a similar method with related row commented and use set_error_handler
before checking $_GET
and $_POST
keys. Also instead, you can use below methods to receive $_GET
, $_POST
and etc keys:
// Could be used in strict mode
function get_($what, $key) {
switch (strtolower($what)) {
case 'get':
return isset($_GET[$key]) ? $_GET[$key] : null;
case 'post':
return isset($_POST[$key]) ? $_POST[$key] : null;
case 'session':
return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
case 'server':
return isset($_SERVER[$key]) ? $_SERVER[$key] : null;
}
}
declare(strict_types=1);
at the top of each (and every) script file, before the namespace declaration. – Countererror_reporting(E_STRICT);
. – Counter