Strict mode in PHP?

2019-01-10 22:26发布

问题:

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 any similar feature?

回答1:

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 a E_NOTICE.

The E_STRICT error livel 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.

Working example that handles only NOTICEs 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

 ?>


回答2:

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.

(Reference)



回答3:

Yes, type error_reporting(E_STRICT|E_ALL); in the beginning of your script.



回答4:

You may check error_reporting, and don't forget to set display_errors as well. Note, that there are multiple levels of error reporting.



回答5:

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



回答6:

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.



回答7:

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.



回答8:

After some years, php 7.0.0 has gained declare(strict_types=1);

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict



回答9:

Yes, you do that with error reporting.

http://www.php.net/manual/en/function.error-reporting.php



回答10:

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 think don't that you want to show PHP error messages to the users but rather allow the script to continue with reduced functionality (eg. 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 to custom error handlers for each environment