Several solutions to php prompt undefined index_PHP tutorial

WBOY
Release: 2016-07-21 15:18:52
Original
968 people have browsed it

Usually when using $_post[''], $_get[''] to obtain parameters in the form, Notice: Undefined index: --------;

We often receive Undefined index errors when receiving data POST from forms, as follows: $act=$_POST['action'];
Using the above code always prompts
Notice: Undefined index: act in D: testpost.php on line 20
In addition, sometimes
Notice: Undefined variable: Submit... and other such prompts appear

The above are not PHP prompts but If an error is reported, PHP itself can be used directly without declaring variables in advance, but there will be a prompt for undeclared variables. Generally, as a formal website, prompts will be turned off, and even error messages will be turned off.

Solution:

Method 1: Server configuration modification
Modify the error display mode under the error configuration in php.ini: Modify error_reporting = E_ALL to

error_reporting = E_ALL & ~E_NOTICE

Restart the APCHE server after modification to take effect.

Method 2: Initialize variables.

Method 3: Make judgment isset($_post['']), empty($_post['']) if --else

Method 4: Add before the notice code appears @, @ means there is an error in this line or a warning not to output, @$username=$_post['username'];
Add an @ in front of the variable, such as if (@$_GET['action']== 'save') { ...

Method 5: The last one is very practical. It is a function written by others, through which values ​​are transferred.

Define a function:

Copy code The code is as follows:

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


Then in When using it, just use _get('str') instead of $_GET['str'] ~

[PHP-Core-Error]

error_reporting = E_ALL & ~E_NOTICE
; The error reporting level is a superposition of bit fields. It is recommended to use E_ALL | Parsing error when
; 8 E_NOTICE Runtime reminder (often a bug, it may be intentional)
; 16 E_CORE_ERROR Fatal error during PHP startup initialization
; 32 E_CORE_WARNING During PHP startup initialization Warning (non-fatal error)
; 64 E_COMPILE_ERROR Fatal error at compile time
; 128 E_COMPILE_WARNING Warning at compile time (non-fatal error)
; 256 E_USER_ERROR Fatal error defined by user
; 512 E_USER_WARNING User-defined warning (non-fatal error)
; 1024 E_USER_NOTICE User-defined reminder (often a bug, may also be intentional)
; 2048 E_STRICT Coding standardization warning (recommend how to modify it to (Pre-compatible)
; 4096 E_RECOVERABLE_ERROR A near-fatal runtime error. If not caught, it will be treated as E_ERROR
; 6143 E_ALL All errors except E_STRICT (8191 in PHP6, including all)


http://www.bkjia.com/PHPjc/325444.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325444.htmlTechArticleUsually using $_post[''], $_get[''] to obtain the parameters in the form, a Notice will appear: Undefined index: --------; We often receive Undefined index errors when receiving data POSTed from forms, as follows:...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template