Strict mode in PHP?

Other languages โ€‹โ€‹with automatic variable declarations - for example, Perl - have strict mode.

When this strict mode is activated, a variable declaration is required, and Perl throws an error as soon as you try to use an undeclared variable.

Does PHP offer any similar function?

+61
variables php declaration
Jul 07 '10 at 8:27
source share
11 answers

View. You can activate the E_NOTICE level in your bug report . (The list of constants is here .)

Each use of the undeclared variable E_NOTICE .

E_STRICT errors E_STRICT will give you these notifications as well as other tips on how to optimize your code.

 error_reporting(E_STRICT); 

Script completion

If you are really serious and want your script to exit rather than just display a notification when an undeclared variable is detected, you can create your own error handler .

A working example that processes only E_NOTICE with an "undefined variable" in them and passes everything else 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 ?> 
+65
Jul 07 '10 at 8:29
source share
โ€” -

Using

 error_reporting(-1); 

to show all possible errors, including E_STRICT and even when new levels and constants are added in future versions of PHP.

( Link )

+39
Jul 07 '10 at 8:36
source share

After a few years, php 7.0.0 received declare(strict_types=1);

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

+5
Feb 22 '18 at 20:57
source share

Yes, enter error_reporting(E_STRICT|E_ALL); at the beginning of your script.

+3
Jul 07 '10 at 8:28
source share

You can check for error_reporting and don't forget to set display_errors . Please note that there are several levels of error reporting.

+2
Jul 07 '10 at 8:30
source share

PHP warns about undeclared variables by default, you just need to rotate the error report so that you see notifications. Note that since there is no special syntax for declaring a variable in PHP, and you simply declare it by assigning it, it can only warn you when you try to use the value of an undeclared variable. Unlike other languages, "assignment to undeclared variables" does not exist, so PHP cannot warn you.

+1
Jul 07 '10 at 8:29
source share

Using

 error_reporting(E_ALL); 

at the beginning of your PHP code. Or set the error_reporting parameter in php.ini file to set it for all PHP files

+1
Jul 07 '10 at 8:30
source share

You can implement your own error handling function with set_error_handler() .

You can then respond to certain levels of errors as you wish.

For example, instead of just showing and logging an error message, you can abort the script if the variable is not declared properly or if some condition that you don't like is met.

Thus, you can apply very strict policies for any code that runs on your instance of the PHP interpreter.

+1
Jul 07 '10 at 8:37
source share
0
Jul 07 '10 at 8:28
source share

I would suggest that reporting and error handling requirements vary within your development environment and your live production environment (WWW, company intranet, etc.). During development, you will want to see as many details as possible in order to find and fix problems.

In a live environment, I think that you do not want to display PHP error messages to users, but allow the script to continue working with reduced functionality (for example, a message like โ€œSorry, we canโ€™t update your profile at the momentโ€ or redirect the user to the home page, etc. .d.). One way to achieve this would be to use custom error handlers for each environment.

0
Jul 07 '10 at 10:26
source share

2019 answer to this old question,

Yes, you can with PHP 7.X onwards,

 declare(strict_types=1); 

This will cause all scalar type declarations to strictly match the types.

But if you enable this globally, it may affect other third-party modules (for example, Composer libraries) that rely on weak mode, so be sure to apply this in the appropriate classes / files.

0
Sep 11 '19 at 5:33
source share



All Articles