I. Related information
Usually use $_post['] or $_get['] to get the parameters in the form will appear notice:undefined index:--------;
And we often receive form post data times undefined index error
For example: $act =$_post[' action ']; Using the above code always prompts notice:undefined index:act in D:\test\post.php to line 20 and sometimes notice also appears: Undefined Variable:submit ... And so some such reminders appear above these are PHP prompts rather than the error, PHP itself does not need to declare variables in advance can be used directly, but not declared variables will be prompted. Generally as a formal website will turn off the prompts, and even the error message is turned off.
Second, the problem description
That is, PHP defaults to prompt for undeclared variables, but this default hint can be ignored
Third, the solution
Method 1: Server Configuration modification
To modify error configuration in php.ini: 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
The definition of a variable is then initialized to it, but it does not determine whether a variable is initialized by event-driven or not.
Method 3: Perform isset ($_post[']), empty ($_post[']) if--else judge
Method 4: Before the notice code appears, add @
@ indicates that the line has errors or warnings do not output for example: @ $username =$_post[' username '; precede the variable with a @, as if (@$_get[' action ']== ' save ') {...
So if this statement appears with a warning reminder, it will not be output.
Method 5: Build yourself a 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 ') instead of $_get[' str ' when using it.
Iv. Analysis and summary
Although PHP provides a good reminder mechanism, it may not be what we want, and we recommend that you do this using method 4 so that you can make sure that you are aware of the reminders and that you retain the reminders that PHP provides
The above content is small to share the PHP tips undefined index How to solve (a variety of methods) of the relevant knowledge, I hope to help you!