Solution to php prompt undefined index

一个新手
Release: 2023-03-15 22:44:01
Original
2130 people have browsed it

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'];
Copy after login

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
Copy after login

Restart the APCHE server after modification to take effect.

Method 2: Initialize variables.

Method 3: Make a judgment

isset($_post['']),empty($_post['']) if --else
Copy after login


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; 
}
Copy after login

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!

Related labels:
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