PHP prompts undefined index how to solve (multiple methods) _php instance

Source: Internet
Author: User
Tags form post

I. Related information

Usually use $_post['] or $_get['] to get the parameters in the form will appear notice:undefined index:--------;

And we often receive form post data times undefined index error

For example: $act =$_post[' action ']; Using the above code always prompts notice:undefined index:act in D:\test\post.php to line 20 and sometimes notice also appears: Undefined Variable:submit ... And so some such reminders appear above these are PHP prompts rather than the error, PHP itself does not need to declare variables in advance can be used directly, but not declared variables will be prompted. Generally as a formal website will turn off the prompts, and even the error message is turned off.

Second, the problem description

That is, PHP defaults to prompt for undeclared variables, but this default hint can be ignored

Third, the solution

Method 1: Server Configuration modification

To modify error configuration in php.ini: Change error_reporting = E_all to error_reporting = E_all & ~e_notice

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

Method 2: Initialize the variable

The definition of a variable is then initialized to it, but it does not determine whether a variable is initialized by event-driven or not.

Method 3: Perform isset ($_post[']), empty ($_post[']) if--else judge

Method 4: Before the notice code appears, add @

@ indicates that the line has errors or warnings do not output for example: @ $username =$_post[' username '; precede the variable with a @, as if (@$_get[' action ']== ' save ') {...

So if this statement appears with a warning reminder, it will not be output.

Method 5: Build yourself a function instead of the value method

The function code is as follows:

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

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

Iv. Analysis and summary

Although PHP provides a good reminder mechanism, it may not be what we want, and we recommend that you do this using method 4 so that you can make sure that you are aware of the reminders and that you retain the reminders that PHP provides

The above content is small to share the PHP tips undefined index How to solve (a variety of methods) of the relevant knowledge, I hope to help you!

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.