: This article mainly introduces several solutions to the prompt undefinedindex. if you are interested in the PHP Tutorial, please refer to it. Several solutions for prompting undefined index in php
I. Related information
Usually used$_post['']
Or$_get['']
When obtaining parameters in the formNotice: Undefined index: --------;
And we often receive data from form POST.Undefined index
Error
For example:
$act=$_POST['action'];
The above code always prompts
Notice: Undefined index: act in D:\test\post.php on line 20
In addition
Notice: Undefined variable: Submit ......
And so on.
The above are PHP prompts rather than errors. PHP itself can be directly used without declaring the variables in advance, but a prompt will be prompted if the variables are not declared. Generally, when a website is a formal website, it will turn off the prompts and even the error message.
II. problem description
That is, PHP prompts undeclared variables by default, but this default prompt can be ignored.
III. solutions
3.1. Method 1: server configuration modification
- Modify
php.ini
Inerror
Configure the error display mode: seterror_reporting = E_ALL
Changeerror_reporting = E_ALL & ~E_NOTICE
- After modification, restart the APCHE server.
3.2. Method 2: initialize the variable
That is, after a variable is defined, it will be initialized, but this does not determine whether a variable is initialized due to event-driven.
3.3. Method 3: determine isset ( Post ['']), empty ( _ Post ["]) if-else
3.4. Method 4: Add @
@ Indicates an error in this line or warning not to output
For example:@$username=$_post['username'];
Add@
, Suchif (@$_GET['action']=='save') { ...
-In this way, if this statement has a warning, no output will be made.
3.5. Method 5: Build a function to replace the value method.
The function code is as follows:
function_get($str){$val = !empty($_GET[$str]) ? $_GET[$str] : null; return$val; }
Then, directly use_get('str')
Replace$_GET['str']
That's all ~
IV. Analysis Summary
Although PHP provides a good reminder mechanism, it may not be what we want. we recommend that you use method 4 to handle it, so that you can handle it when you observe the reminder, the reminder mechanism provided by PHP is also retained.
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above describes several solutions to the prompt undefined index, including some content, and hope to be helpful to friends who are interested in PHP tutorials.