PHP prompts undefined index how to resolve (multiple methods) _php instances

Source: Internet
Author: User
I. Related INFORMATION

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

Well we often receive forms 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 Additionally, sometimes notice appears: Undefined Variable:submit ... Wait for some of these reminders to appear above these are the PHP prompt instead of the error, PHP itself does not need to declare variables in advance to use directly, but the non-declared variables will be prompted. 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

Method 1: Server configuration Modifications

To modify the error display in the php.ini configuration: 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

That is, when a variable is defined, it is initialized specifically, but it does not determine whether a variable is initialized by event-driven.

Method 3: Do Isset ($_post["]), Empty ($_post[")) if--else judged

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 an @, such as if (@$_get[' action ']== ' save ') {...

This will not output if the statement has a warning alert

Method 5: Build your own 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 ') directly instead of $_get[' str ').

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, which can be done when the reminder is observed, but also preserves the notification mechanism provided by PHP

The above content is a small series to share the PHP tips undefined index How to solve (a variety of methods) of the relevant knowledge, 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.