Several solutions to undefined index in PHP
I. Related information
$_post['']
$_get['']
It appears when you use or get the parameters in the formNotice: Undefined index: --------;
And we often receive the form 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
In addition, there are sometimes
Notice: Undefined variable: Submit ......
Wait for some reminders like this
These are the PHP tips instead of an error, and PHP itself does not need to declare variables in advance to be used directly, but there are hints for undeclared variables. 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
3.1. Method 1: Server Configuration modification
- Modify
php.ini
The error
configuration under the error display mode: Change the error_reporting = E_ALL
revision toerror_reporting = E_ALL & ~E_NOTICE
- After the modification, restart the Apche server before it can take effect.
3.2. 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.
3.3, Method 3: Make a judgment isset ( post[′′] ),empty( _ post["]) if–else
3.4. 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 one @
, such asif (@$_GET['action']=='save') { ...
-This will not output if there is a warning alert in this statement
3.5, Method 5: Build a function instead of the value method
The function code is as follows:
function_get($str){$val = !empty($_GET[$str$_GET[$strnullreturn$val
Then in use, the direct use of _get('str')
instead of the $_GET['str']
line ~
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, so as to ensure that the reminder is observed in the processing, but also preserves the PHP provides a reminder mechanism
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes a hint undefined index of several solutions, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.