PHP Hint notice:undefined index solution

Source: Internet
Author: User

PHP hint notice:undefined index problem, Undefined index: refers to the existence of your code: "Variables are not defined, assigned to use" error, this is not a fatal error, will not let your PHP code run forcibly aborted, but there is a potential problem of the danger ......

Occurs when reading data:

Notice:undefined Index:name in ...

Notice:undefined Index:key in ...

The source code is as follows:

$name = isset ($_post[' name ')? Filter_input (input_post, ' name ', Filter_sanitize_special_chars): Htmlspecialchars ($_get[' name ');

$key = isset ($_post[' key ')? Filter_input (input_post, ' key ', Filter_sanitize_special_chars): Htmlspecialchars ($_get[' key ');

Problem Analysis:

Undefined index: Refers to the existence of your code: "Variables are not defined, assigned to use" error, this is not a fatal error, will not let your PHP code run forcibly aborted, but there is a potential problem of the risk, it is recommended to modify ~ ~ ~

Workaround:

with php.ini error_reporting = E_all & ~e_notice can turn off the display of NOTICE, shielding out such warnings good, however, the proposal or change the code a little better, code always write a good specification, the future can be less problems ah.

Problem reason: Because you only check $_post existence, but do not check the existence of $_get.

the perfect solution : modify it as shown below:

$name = isset ($_post[' name ')? Filter_input (input_post, ' name ', Filter_sanitize_special_chars): Isset ($_get[' name '])? Filter_input (input_post, ' name ', filter_sanitize_special_chars): ';

$key = isset ($_post[' key ')? Filter_input (input_post, ' key ', Filter_sanitize_special_chars): Isset ($_get[' key ')? Filter_input (input_post, ' key ', filter_sanitize_special_chars): ';

First of all, this is not a mistake, it is warning. Therefore, if the server cannot be changed, each variable should be defined before use.


Method 1: Server configuration Modifications
Modify the php.ini configuration file, error_reporting = E_all & ~e_notice


Method 2: Initialize the variables, standardize the writing (more cumbersome, because there are a lot of variables). But have not found a good definition method, I hope you advise


Method 3: Each file header plus: error_reporting (0); If not, only open php.ini, find display_errors, set to Display_errors = Off. Any future errors will not be prompted.


Method 4: Make a Judgment: isset ($_get["page"]) If-else judgment

or add "@" to indicate this line if there is an error or warning do not output
such as: @ $page =$_get["page"]


Method 5:file1.php file to pay a value $xx variable, with post to file2.php,
If the file2.php does not have the definition of $xx, the direct use of $yy= $xx; The system will be error: "Undifined variaable $xx", if file2.php's files start with $xx= ""; definition, then file1.php $xx value will not pass!


File2.php can do that.
if (!isset ($xx)) $xx = "";

========================== Method 3: Each file header plus: error_reporting (0); Solve

PHP Hint notice:undefined index solution

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.