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.
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; }
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool