Php prompts several solutions for undefined index

Source: Internet
Author: User
Tags form post

When $ _ post [''] and $ _ get [''] are used to obtain parameters in the formNotice: Undefined index :--------;

We often receive Undefined index errors when receiving data from form POST, as follows: $ 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.

Solution:

Method 1: Modify server configurations
Modify the error display mode in the error configuration in php. ini: Change error_reporting = E_ALL

Error_reporting = E_ALL &~ E_NOTICE

After modification, restart the APCHE server.

Method 2: Initialize the variable.

Method 3: Judge isset ($ _ post ['']), empty ($ _ post ['']) if -- else

Method 4: Add @ before the notice code appears. @ indicates that the line has an error or the warning is not output. @ $ username = $ _ post ['username'];
Add a @ in front of the variable, such as if (@ $ _ GET ['action'] = 'save '){...

Method 5: The last one is very practical. It is a function written by someone else, through which values are transmitted.

Define a function:
Copy codeThe Code is as follows:
Function _ get ($ str ){
$ Val =! Empty ($ _ GET [$ str])? $ _ GET [$ str]: null;
Return $ val;
}


Then, replace $ _ get ['str'] With _ GET ('str ~

[PHP-Core-Error]

Error_reporting = E_ALL &~ E_NOTICE
The error report level is the superposition of bit fields. We recommend that you use E_ALL | E_STRICT
; 1 E_ERROR fatal runtime error
; 2 E_WARNING runtime warning (non-fatal error)
4. parsing error during E_PARSE Compilation
; 8 E_NOTICE runtime reminder (often a bug or intentionally)
; 16 E_CORE_ERROR fatal error during PHP startup
32 _ core_warning warning during initialization (non-fatal error) during PHP startup)
; 64 E_COMPILE_ERROR fatal error during compilation
; 128 E_COMPILE_WARNING warning during compilation (non-fatal error)
256 User-Defined fatal error
; 512 user_warning User-Defined warning (non-fatal error)
1024 E_USER_NOTICE User-Defined notifications (often bugs, or intentionally)
; 2048 E_STRICT code standardization warning (how to modify to forward compatibility)
; 4096 E_RECOVERABLE_ERROR is close to a fatal runtime error. If it is not captured, it is treated as E_ERROR.
; 6143 all except E_STRICT errors (8191 in PHP6, that is, all errors)

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.