Home > Backend Development > PHP Tutorial > How to solve the problem of undefined index in php

How to solve the problem of undefined index in php

不言
Release: 2023-04-01 14:24:01
Original
3923 people have browsed it

Although this prompt can be hidden by setting the error display mode, this also has hidden dangers, that is, these prompts will be recorded in the server's log, causing the log file to be abnormally large.

Usually use $_post[''] ,$_get[''] will appear when getting the parameters in the formNotice: Undefined index: --------;

We often receive POST from the form The data reports an Undefined index error, 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 PHP prompts rather than errors. PHP itself does not need to declare variables in advance. It can be used directly, 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, @ represents this line Do not output if there is an error or warning, @$username=$_post['username'];
Add an @ in front of the variable, such as if (@$_GET['action']=='save') { . ..
Method 5: The last method is very practical. It is a function written by others, and the value is transferred through this function.
Define a function:

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

Then 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 the superposition of bit fields. It is recommended to use E_ALL | E_STRICT
; 1 E_ERROR fatal runtime error
; 2 E_WARNING runtime warning (non-fatal sexual error)
; 4 E_PARSE Compile-time parsing error
; 8 E_NOTICE Run-time reminder (often a bug, may be intentional)
; 16 E_CORE_ERROR Fatal error during PHP startup initialization process
; 32 E_CORE_WARNING Warning during PHP startup initialization process (non-fatal error)
; 64 E_COMPILE_ERROR Fatal error during compilation
; 128 E_COMPILE_WARNING Warning during compilation (non-fatal error)
; 256 E_USER_ERROR User-defined fatal error
; 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 encoding standardization warning (recommended how to modify for forward compatibility)
; 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 (in PHP6 8191, that is, including all)

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Use strip_tags() in php to remove html tags and still have blank solutions

##Use PHP How to get the real IP of the user client

The above is the detailed content of How to solve the problem of undefined index in php. 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