Error message about PHPUndefinedindex _ PHP Tutorial

Source: Internet
Author: User
Error message about PHPUndefinedindex. When you GET data in the form of $ _ GET [xx], if you do not add a judgment before, $ _ GET [xx] will receive the following warning when it does not exist: PHPNotice: undefinedindexxxx. Although you can GET data by setting an error in the form of $ _ GET ["xx, when $ _ GET ["xx"] does not exist, the following warning occurs: PHP Notice: undefined index xxx.

Although you can hide this prompt by setting the error display mode, there is also a risk that these prompts will be recorded in the server logs, resulting in a huge log file exception.

First, this is not an error, it is warning. Therefore, if the server cannot be changed, each variable should be defined before use. There are several popular solutions on the Internet:

  1. Method 1: modify the server configuration. Modify the php. ini configuration file, error_reporting = E_ALL &~ E_NOTICE.
  2. Method 2: initialize the variables and write them in a standardized manner (complicated because there are a large number of variables ). However, we have not found a proper definition method.
  3. Method 3: Add error_reporting (0) to the header of each file. if not, open php. ini, find display_errors, and set display_errors to Off. No errors will be prompted in the future.
  4. Method 4: Determine if-else: isset ($ _ GET ["page. Or add '@' to indicate that this line will not be output if there is an error or warning. For example: @ $ page = $ _ GET ["page"]
  5. Method 5: the file1.php file pays a value for the $ xx variable and uses post to pass it to file2.php. if file2.php does not have a $ xx definition, it directly uses $ yy = $ xx; the system reports the error "undifined variaable $ xx". if the file2.php file is defined with $ xx = "";, the $ xx value of file1.php cannot be passed. In file2.php, you can: if (! Isset ($ xx) $ xx = "";

If you think the above method is not very useful, you can also use the following method:

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

Define such a function. when using this function, replace $ _ get ['str'] with _ GET ('str ~ This makes it easier.

When using "xx"] to obtain data in this form, if no judgment is made before, the following warning will appear when $ _ GET ["xx"] does not exist: PHP Notice: undefined index xxx. Although you can set an error...

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.