1 Notice: Undefined index: ZZZZZZWTF? [duplicate]
When running the PHP script again, it is clear that there is no problem with the script syntax, but there are the following prompts:
Notice: Undefined index: submit in C:\xampp\htdocs\globalautoparts\register.php on line 36 Notice: Undefined index: fullname in C:\xampp\htdocs\globalautoparts\register.php on line 40 Notice: Undefined index: username in C:\xampp\htdocs\globalautoparts\register.php on line 41 Notice: Undefined index: password in C:\xampp\htdocs\globalautoparts\register.php on line 42 Notice: Undefined index: repeatpassword in C:\xampp\htdocs\globalautoparts\register.php on line 43 Notice: Undefined index: email in C:\xampp\htdocs\globalautoparts\register.php on line 45
In this case, how to solve it.
2 solution
Modify the log prompt level of PHP and ignore these prompts. Add the following statement at the top of the script, and then solve the problem.
error_reporting(E_ALL ^ E_NOTICE);
3 error_reporting() function
3.1 definition and usage
error_ The reporting () function specifies which error to report.
This function sets the error reporting level of the current script.
This function returns the old error reporting level.
3.2 error_ Several common ways of writing in reporting ()
- error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING): this means reporting except E_ NOTICE, E_ All errors except warning
- error_reporting(E_WARNING | E_ERROR): this means that only e is reported_ Error and e_ Warning these two levels of error
- error_ Reporting (E _all & ~ e _note): the same as ^ for reporting except E_ All errors except note
3.3 log level in PHP
value | constant | explain | Remarks (version requirements) |
---|---|---|---|
1 | E_ERROR | Fatal runtime error. Such errors are generally unrecoverable, such as problems caused by memory allocation. The result is that the script terminates and does not continue to run. | |
2 | E_WARNING | Runtime warning (non fatal error). Only prompts are given, but the script does not terminate. | |
4 | E_PARSE | Compile time syntax parsing error. Parsing errors are only generated by the parser. | |
8 | E_NOTICE | Runtime notification. It indicates that the script may encounter errors, but there may be similar notifications in the script that can run normally. | |
16 | E_CORE_ERROR | A fatal error occurred during PHP initialization startup. This error is similar to E_ERROR, but it is generated by the PHP engine core. | since PHP 4 |
32 | E_CORE_WARNING | Warning (non fatal error) during PHP initialization startup. Similar to E_WARNING, but it is generated by the PHP engine core. | since PHP 4 |
64 | E_COMPILE_ERROR | Fatal compile time error. Similar to E_ERROR, but it is generated by Zend script engine. | since PHP 4 |
128 | E_COMPILE_WARNING | Compile time warning (non fatal error). Similar to E_WARNING, but it is generated by Zend script engine. | since PHP 4 |
256 | E_USER_ERROR | User generated error messages. Similar to E_ERROR, but the user himself uses the PHP function trigger in the code_ Error(). | since PHP 4 |
512 | E_USER_WARNING | User generated warning messages. Similar to E_WARNING, but the user himself uses the PHP function trigger in the code_ Error(). | since PHP 4 |
1024 | E_USER_NOTICE | User generated notification information. Similar to e_ Note, but the user himself uses the PHP function trigger in the code_ Error(). | since PHP 4 |
2048 | E_STRICT | Enable PHP to suggest changes to the code to ensure the best interoperability and forward compatibility of the code. | since PHP 5 |
4096 | E_RECOVERABLE_ERROR | Fatal errors that can be caught. It indicates that a potentially dangerous error has occurred, but it has not caused the PHP engine to be in an unstable state. If the error is not captured by the user-defined handle (see | set_error_handler() will become an E_ERROR so that the script will terminate. since PHP 5.2.0 |
8192 | E_DEPRECATED | Runtime notification. When enabled, warnings will be given to code that may not work properly in future releases. | since PHP 5.3.0 |
16384 | E_USER_DEPRECATED | Warning message for low user productivity. Similar to E_DEPRECATED, but the user himself uses the PHP function trigger in the code_ Error() | since PHP 5.3.0 |
32767 | E_ALL | E_ All errors and warnings outside strict. | 32767 in PHP 5.4.x, 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously |
3.4 about E_ALL and E_STRICT
As you can see in the table above, in PHP5 In versions 3 and below, e_ The value of all is 30719 because PHP5 E of 3_ All does not contain E_STRICT level error, but in PHP5 4, E_ALL has defaulted to E_STRICT is included
This is because of the PHP version upgrade process, E_STRICT is a strict mode. For example, we all know that static methods in classes should be static only, and non static methods need to instantiate classes. Later, PHP also supports the use of: a static way to call a non static method, but at this time, if we start a strict mode E_STRICT; Will result in an E_STRICT level error
The following code, in PHP5 There is no problem running in 3, but in PHP5 An error will be reported in 4
ini_set("display_errors", true); error_reporting(E_ALL); class a { public function test() { echo 1111; } } a::test();
The above code, if in PHP5 3, then there is no problem if it is in PHP5 4 and above, the following error will be reported
Strict standards: Non-static method a::test() should not be called statically in ....
So you will see error in some lower versions of php code_ Reporting (e_all | e_strict)
3.5 principle of error level in PHP
The error level of php is essentially stored in bitmap method. Each bit of binary is used to represent a state. For example, a byte has 8 bits. By distinguishing 01 from each bit of the 8 big, it can represent 8 states. Back to php, let's disassemble the error mode of php and look at the following pictures ↓↓
We modify the value of errorlevel, that is, modify the state of the corresponding bit.
3.5.1 or operation mode to accumulate different error levels
The rule of or operation is that one party is 1, the result is 1, both are 0, and the result is 0
Here we set only E_ERROR and e_ Note these two levels of errors
ini_set("display_errors", true); $errorLevel = E_ERROR | E_NOTICE; error_reporting($errorLevel); // Print the value of $errorLevle echo $errorLevel; // 9
In the above code, we set the error level value to 9, and php will only report E_ERROR and e_ Note these two levels of errors will be masked. What is the internal mechanism of php? Let's take a look
/** E_ERROR Value of = 1; The corresponding binary is 0000 0001 E_NOTICE Value of = 8; The corresponding binary is 0000 1000 After the bitwise OR operation of these two values--------- The result is 0000 1001 The corresponding number converted to hexadecimal is 9 In php, to determine whether it is the corresponding error level, you only need to use [corresponding error level] & [set error level] Let's assume that php has a note level error during operation, and judge whether the error level is enabled */ if (E_NOTICE & $errorLevel) { // The note level is enabled to handle errors } /** In the above code, just use bitwise&, You can judge E_NOTICE Value of = 8; The corresponding binary is 0000 1000 $errorLevel Value of = 9; The corresponding binary is 0000 1001 --------- The result of the operation is 80000 1000 Then if you use E_WARNING To judge, please see below E_WARNING Value of = 2; The corresponding binary is 0000 0010 $errorLevel Value of = 9; The corresponding binary is 0000 1001 --------- The result after the operation is 0 0000 0000 As you can see, the result is 0, which does not match the current error level
Through the above description and the above picture, it can be found that it is essentially to modify the state of the corresponding bit. When using, different operations are carried out by judging the state of the bit
3.5.2 exclude a state by XOR
Lazy, write less, you can refer to or calculate, which is essentially the same
The XOR rule is: the same is 0 and the difference is 1
// Use E_ALL, and E is excluded_ Note and E_WARNING, it can be written like this $errorLevel = E_ALL ^ E_NOTICE ^ E_WARNING; error_reporting($errorLevel); /** What is their calculation process? Let's have a look E_ALL Value of = 32767; The corresponding binary is 1111 1111 1111 1111 E_NOTICE Value of = 8; The corresponding binary is 0000 1000 ------------------- The result is 1111 1111 1111 0111 Looking at the above results, is it found that the state of the binary bit corresponding to 8 has changed to 0, and the other bit states have not changed, So it's equivalent to excluding 8, that is E_NOTICE, Let's use this result to compare with E_WARNING XOR The result of the previous step is 1111 1111 1111 1111 E_WARNING Value of = 2; 0000 0000 0000 0010 ------------------- The result is 1111 1111 1111 0101 You can see, put E_WARNING The corresponding status is also set to 0
3.5.3 take the inverse first and then connect with&
The result is XOR. I won't say more here
Reference
- https://stackoverflow.com/questions/7115852/notice-undefined-index-zzzzzzwtf
- https://www.runoob.com/php/func-error-reporting.html
- https://www.php.net/manual/en/function.error-reporting.php
- https://www.xstnet.com/article-148.html
Write at the end
Welcome to the official account of the public. zhg, the catcher in the rye, let's grow up together. Thank you.