Strict mode in PHP
PhpVariablesDeclarationPhp Problem Overview
Other languages with automatic variable declaration - like Perl - have a strict mode.
By activating this strict mode, variable declaration is required, and Perl throws an error as soon as you try to use an undeclared variable.
Does PHP offer a similar feature?
Php Solutions
Solution 1 - Php
Kind of. You can activate the E_NOTICE
level in your error reporting. (List of constants here.)
Every instance of usage of an undeclared variable will throw an E_NOTICE
.
The E_STRICT
error level will also throw those notices, as well as other hints on how to optimize your code.
error_reporting(E_STRICT);
Terminating the script
If you are really serious, and want your script to terminate instead of just outputting a notice when encountering an undeclared variable, you could build a custom error handler.
A working example that handles only E_NOTICE
s with "Undefined variable" in them and passes everything else on to the default PHP error handler:
<?php
error_reporting(E_STRICT);
function terminate_missing_variables($errno, $errstr, $errfile, $errline)
{
if (($errno == E_NOTICE) and (strstr($errstr, "Undefined variable")))
die ("$errstr in $errfile line $errline");
return false; // Let the PHP error handler handle all the rest
}
$old_error_handler = set_error_handler("terminate_missing_variables");
echo $test; // Will throw custom error
xxxx(); // Will throw standard PHP error
?>
Solution 2 - Php
Use
error_reporting(-1);
to show every possible error, including E_STRICT
and even when new levels and constants are added in future PHP versions.
Solution 3 - Php
After some years, PHP 7.0.0 has
gained declare(strict_types=1)
.
Solution 4 - Php
Yes, type error_reporting(E_STRICT|E_ALL);
in the beginning of your script.
Solution 5 - Php
You may check error_reporting
, and don't forget to set display_errors
as well.
Note, that there are multiple levels of error reporting.
Solution 6 - Php
PHP is warning about undeclared variables by default; you just need to turn the error reporting level up so you'll see the notices. Note though that since there's no special syntax to declare a variable in PHP and you simply declare one by assigning to it, it can only warn you when you try to use the value of an undeclared variable. Contrary to other languages, "assignments to undeclared variables" do not exist, so PHP can't warn you there.
Solution 7 - Php
Use
error_reporting(E_ALL);
at the beginning of your PHP code.
Or set the error_reporting setting in your php.ini file, to set it for all PHP files.
Solution 8 - Php
You can implement your own error handling function with set_error_handler()
.
Then you can react to certain error levels as you wish.
For example, instead of just showing and logging an error message, you could terminate the script if a variable is not declared properly or if any condition is met that you don't like.
That way you can enforce a very strict policy for any code that runs on your PHP interpreter instance.
Solution 9 - Php
Yes, you do that with error reporting.
Solution 10 - Php
I would suggest that the requirements for reporting and handling errors differ within your development environment and your live production environment (WWW, company intranet, etc.). During development you will want to see as much detail as possible to find and fix problems.
In the live environment, I don't think that you want to show PHP error messages to the users, but rather allow the script to continue with reduced functionality (for example, a message like "Sorry we cannot update your profile at the moment", or redirect the user to the home page, etc.). A way to achieve this would be through the use of custom error handlers for each environment.
Solution 11 - Php
Yes, you can from PHP 7.X onwards,
declare(strict_types=1);
This will enforce all the scalar type declarations to be strict with types.
But if you enable this globally, it can affect other third-party modules (for example, PHP Composer libraries) which are relying in weak mode, so make sure to enforce it in relevant classes/files.
Solution 12 - Php
Use:
error_reporting(E_STRICT);
I think you need to try this above.
Solution 13 - Php
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;
}
}