I. Related INFORMATION
$_post['] or $_get['] will appear when you get the parameters in the form notice:undefined index:--------;
Well we often receive forms post come over the data times undefined index error
For example: $act =$_post[' action ']; Using the above code will always prompt notice:undefined index:act in D:\test\post.php on line 20 Additionally, sometimes notice appears: Undefined Variable:submit ... Wait for some of these reminders to appear above these are the PHP prompt instead of the error, PHP itself does not need to declare variables in advance to use directly, but the non-declared variables will be prompted. Generally as a formal website will be the prompt to turn off, and even the error message is also turned off.
Ii. Description of the problem
That is, PHP defaults to the non-declared variables, but this default hint we can be ignored
Third, the solution
Method 1: Server configuration Modifications
To modify the error display in the php.ini configuration: Change error_reporting = E_all to error_reporting = E_all & ~e_notice
After the modification, restart the Apache server before it can take effect.
Method 2: Initialize the variable
That is, when a variable is defined, it is initialized specifically, but it does not determine whether a variable is initialized by event-driven.
Method 3: Do Isset ($_post["]), Empty ($_post[")) if--else judged
Method 4: Add @ Before the notice code appears
@ indicates that the line has errors or warnings do not output for example: @ $username =$_post[' username '); precede the variable with an @, such as if (@$_get[' action ']== ' save ') {...
This will not output if the statement has a warning alert
Method 5: Build your own function instead of the value method
The function code is as follows:
function _get ($str) {$val =!empty ($_get[$str])? $_get[$STR]: null; return $val;}
Then use _get (' str ') directly instead of $_get[' str ').
Iv. Analysis and summary
Although PHP provides a good reminder mechanism, but may not be what we want, it is recommended to use Method 4 for processing, which can be done when the reminder is observed, but also preserves the notification mechanism provided by PHP
The above content is a small series to share the PHP tips undefined index How to solve (a variety of methods) of the relevant knowledge, hope to help you!