3. several solutions for prompting undefinedindex in PHP

Source: Internet
Author: User
3. several solutions for undefinedindex prompt in php I. Related information
  • When you use $ _ post [''] or $ _ get [''] to obtain parameters in a form, the following error occurs: Notice: Undefined index :--------;
  • And the Undefined index error is often reported when we receive data from form POST.

For example: $ act = $ _ POST ['action']; when you use the above code, the system always prompts Notice: Undefined index: act in D: \ test \ post. php on line 20 In addition, sometimes 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. Solution 1: server configuration modification
  1. Modify the error display mode in the error configuration in php. ini: change error_reporting = E_ALL to error_reporting = E_ALL &~ E_NOTICE
  2. The modification takes effect only after the Apache server is restarted.
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.

Method 3: perform isset ($ _ post ['']), empty ($ _ post ['']) if -- else judgment method 4: Add @ Before the notice code appears @

@ Indicates that this line has errors or warnings. for example, @ $ username = $ _ post ['username']. add @ in front of the variable @, for example, if (@ $ _ GET ['action'] = 'save '){...

  • In this way, if this statement has a warning, no output will be made.
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, replace $ _ get ['str'] with _ GET ('str ~

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 the problem.In this way, the reminder mechanism provided by PHP can be retained while the reminder is observed.

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.