PHP tips for undefined index resolution

Source: Internet
Author: User
When you use $_post['],$_get[' to get the parameters in the form Notice: Undefined index:--------;

We often receive forms post over the data Times undefined index error, as follows:

$act =$_post[' action '];

Always prompt with the above code
notice:undefined index:act in D:\test\post.php on line 20
In addition, there are sometimes
Notice:undefined Variable:submit ... Wait for some of these tips

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.

Workaround:

Method 1: Server configuration Modifications
To modify the error display in php.ini in the errors configuration:

Modify error_reporting = E_all to error_reporting = E_all & ~e_notice

After the modification, restart the Apche server before it can take effect.

Method 2: Initialize the variable.

Method 3: Make a Judgment

Isset ($_post["]), Empty ($_post[")) if--else


Method 4: Add @,@ before the notice code to indicate that the line has errors or warnings do not output, @ $username =$_post[' username '];
Precede the variable with an @, such as if (@$_get[' action ']== ' save ') {...

Method 5: The last one is very practical, is a function written by others, through this function to pass the value.

Define a function:

The code is as follows:

function _get ($str) {$val =!empty ($_get[$str])? $_get[$STR]: null; return $val;}

Then use _get (' str ') instead of $_get[' str ').

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.