Several solutions to undefined index

Source: Internet
Author: User
Tags form post
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

    1. Modify php.ini The error configuration under the error display mode: Change the error_reporting = E_ALL revision toerror_reporting = E_ALL & ~E_NOTICE
    2. 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.