PHP prompts how to solve the undefined index (multiple methods), undefinedindex

Source: Internet
Author: User
Tags form post

PHP prompts how to solve the undefined index (multiple methods), undefinedindex

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 ...... some of the above prompts are PHP prompts rather than errors. PHP itself does not need to declare variables in advance and can be used directly, but there will be prompts for un-declared variables. 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. Solutions

Method 1: Modify server configurations

Modify the error display mode in the error configuration in php. ini: Change error_reporting = E_ALL to error_reporting = E_ALL &~ E_NOTICE

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 ['']) and 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 issue, so that you can handle it when you observe the reminder, the reminder mechanism provided by PHP is also retained.

The above content is a small series of PHP prompts you how to solve the undefined index (multiple methods) related knowledge, I hope to help you!

Articles you may be interested in:
  • Php Undefined index and Undefined variable solution
  • Php Undefined index
  • Php Notice: Undefined index error prompt solution
  • Notice: Undefined index: page in E: \ PHP \ test. php on line 14
  • PHP Undefined index error Repair Method
  • Php prompts several solutions for undefined index

Related Article

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: 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.