How to solve php undefined index error problem

藏色散人
Release: 2023-03-05 06:20:01
Original
4375 people have browsed it

Solution to php index error: 1. Modify the error display mode under the error configuration in php.ini; 2. Initialize the variables; 3. Perform "isset($_post['']), empty($_post['']) if --else" judgment.

How to solve php undefined index error problem

Recommended: "PHP Video Tutorial"

How to solve the undefined index prompt in PHP (various Method)

This article mainly introduces the relevant information on how to solve the undefined index prompt in PHP (multiple methods). Friends in need can refer to the following

1. Related information

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

And we often When receiving the data from the form POST, an Undefined index error is reported.

For example: $act=$_POST['action']; Using the above code will always prompt Notice: Undefined index: act in D:\test\post. php on line 20 In addition, sometimes Notice: Undefined variable: Submit... and other such reminders appear. The above are PHP prompts rather than errors. PHP itself does not need to declare variables in advance and 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.

2. Problem description

That is, PHP will prompt for undeclared variables by default, but we can ignore this default prompt

3. 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

Modify Then restart the Apache server for it to take effect.

Method 2: Initialize the variable

That is, after defining a variable, perform specific initialization on it, but this cannot determine whether a variable has been initialized due to event driving.

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

Method 4: Add before the notice code appears @

@ indicates that there is an error in this line or a warning not to output. For example: @$username=$_post['username']; add an @ in front of the variable, such as if (@$_GET['action' ]=='save') { ...

In this way, if a warning occurs in this statement, it will not be output.

Method 5: Build a function yourself to replace the value method

The function code is as follows:

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']~

4. Analysis Summary

Although PHP provides a good reminder mechanism, it may not be what we want. It is recommended to use method 4 above for processing. This can ensure that the reminder is processed when the reminder is observed, and also retains PHP Reminder mechanism provided

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