views:

127

answers:

9

Hey,

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?

Thx

+1  A: 

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

chelmertz
+1  A: 

Yes, you do that with error reporting.

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

Palantir
+8  A: 

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

 ?>
Pekka
yes it is kind of but not as a whole like other strict-type languages.
Sarfraz
@sAc yup, there are other things that are output as notices too, so they will come up as well when you turn this on. (And obviously, it's not as strict as in a strongly typed language.)
Pekka
Thanks your code works excellent! I changed `die` into `throw` to redirect the output to the Zend Error Trace view.
Ghommey
+1  A: 

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.

deceze
+1  A: 

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

JochenJung
+1  A: 

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

galambalazs
+4  A: 

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.

Gordon
+1 for that -1 thing and future compatibility.
Sarfraz
+1  A: 

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.

Techpriester
A: 

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

Vincent
I implemented Pekkas code into my testing / development stages.
Ghommey