Home >Backend Development >PHP Tutorial >Several solutions to php prompt undefined index_PHP tutorial
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: