Several solutions to undefinedindex prompt

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

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

  1. Modifyphp.iniInerrorConfigure the error display mode: seterror_reporting = E_ALLChangeerror_reporting = E_ALL & ~E_NOTICE
  2. 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.

    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.