Usually using $_post[''], $_get[''] to obtain the parameters in the form will appear Notice: Undefined index: --------;
We often receive Undefined index errors when receiving data from POST forms, as follows:
$act=$_POST['action'];
Using the above code always prompts
Notice: Undefined index: act in D: \test\post.php on line 20
In addition, sometimes
Notice: Undefined variable: Submit... and other such prompts will appear
The above are for PHP A prompt instead of an error. 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:
将error_reporting = E_ALL 修改为 error_reporting = E_ALL & ~E_NOTICE
Restart the APCHE server after modification to take effect.
Method 2: Initialize variables.
Method 3: Make a judgment
isset($_post['']),empty($_post['']) if --else
Method 4: Add @ before the notice code appears, @ indicates that 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 This is very practical. It is a function written by someone else, and values are transferred through this function.
Define a function:
The code is as follows:
function _get($str){ $val = !empty($_GET[$str]) ? $_GET[$str] : null; return $val; }
Then when using it, directly use _get('str') instead of $_GET['str '] That's it~
The above is the detailed content of Solution to php prompt undefined index. For more information, please follow other related articles on the PHP Chinese website!