php7 new addition will not report undefined Solution: 1. Change the error reporting level of PHP by modifying the php.ini file; 2. Use the error_reporting function in the code to dynamically set the error reporting level; 3. Use functions such as isset and empty to check whether the variable has been determined; 4. Use PHP's strict mode to force variables to be declared before use, so that potential problems can be discovered and repaired earlier.
The operating environment of this article: Windows 10 system, PHP7.4 version, Dell G3 computer.
In PHP 7, when we access an undefined variable or call an undefined function, an error will no longer be reported. Instead, an E_NOTICE level error will be thrown and a NULL value will be returned. This change in behavior is intended to improve code robustness and readability.
However, for some developers, they may hope to still get undefined variable errors in PHP 7 so that potential problems can be discovered and fixed in time. Fortunately, PHP 7 provides some options to control this behavior.
1. Change the error reporting level of PHP by modifying the php.ini file.
In the php.ini file, you can find a configuration item called error_reporting, which determines which errors PHP should report. By default, the value of this configuration item is E_ALL & ~E_NOTICE, which means that PHP will report all errors except E_NOTICE level errors. We can modify this to E_ALL to enable reporting of all errors, including errors for undefined variables.
2. Use the error_reporting function in the code to dynamically set the error reporting level.
For example, we can use the following statement at the beginning of the code to enable the reporting of all errors:
error_reporting(E_ALL);
3. Use functions such as isset and empty to check whether the variable has been definition.
These functions can help us judge before accessing variables and avoid using undefined variables.
4. Use PHP's strict mode to force variables to be declared before use.
Strict mode can be enabled by adding the following statement at the beginning of the code:
declare(strict_types=1);
In strict mode, PHP will throw an error when accessing an undefined variable and not It is automatically initialized to NULL. This helps us detect and fix potential problems earlier.
Summary
If we want to still get the error of undefined variables in PHP 7, we can change the error by modifying the php.ini file or using the error_reporting function Report level, use functions like isset and empty to check if a variable is defined, and enable strict mode to enforce declaration of variables. These methods can help us discover and fix potential problems in a timely manner during the development process, and improve the robustness and readability of the code.
The above is the detailed content of What should I do if the new addition in php7 does not report undefined?. For more information, please follow other related articles on the PHP Chinese website!