PHP中的严格模式?

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?

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_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

 ?>

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

Yes, you do that with error reporting.

http://www.php.net/manual/en/function.error-reporting.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.

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

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

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)

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.

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

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