


PHP error shielding error_reporting detailed explanation_PHP tutorial
In Windows environment: The program was originally running normally in PHP tutorial 4.3.0. Why are there many errors reported in 4.3.1? The general prompt is: Notice: Undefined varialbe: variable name.
For example, there is the following code:
Copy the code. The code is as follows:
if (!$tmp_i) {
$tmp_i=10;
}
It runs normally in 4.3.0. When running in 4.3.1, Notice:Undefined varialbe:tmp_i
Next question:
1. What is the problem?
2. How should this code be modified?
3. Without changing the code, how to modify the settings in php.ini so that the original program in 4.3.0 can run normally in the 4.3.1 environment? This error message will not appear.
Solution:
Add a sentence at the beginning of the program:
error_reporting(E_ALL & ~E_NOTICE); or error_reporting(E_ALL ^ E_NOTICE);
or
Modify php.ini
error_reporting = E_ALL & ~E_NOTICE
About error_reporting() function:
error_reporting() sets PHP's error reporting level and returns the current level.
; Error reporting is bitwise. Or add up the numbers to get the desired error reporting level.
; E_ALL - all errors and warnings
; E_ERROR - fatal runtime error
; E_WARNING - runtime warning (non-fatal error)
; E_PARSE - compile-time parsing error
; E_NOTICE - runtime reminder (these are often caused by bugs in your code,
;It may also be caused by intentional behavior. (For example: automatically initialized to a
based on uninitialized variables
;The fact that an empty string is used instead of an uninitialized variable)
; E_CORE_ERROR - Fatal error that occurred during the initialization process when PHP started
; E_CORE_WARNING - Warning (non-fatal error) that occurs during the initialization process of PHP startup
; E_COMPILE_ERROR - fatal compile-time error
; E_COMPILE_WARNING - compile-time warning (non-fatal error)
; E_USER_ERROR - user generated error message
; E_USER_WARNING - user generated warning message
; E_USER_NOTICE - user generated reminder message
How to use:
error_reporting(0);//Disable error reporting
error_reporting(E_ALL ^ E_NOTICE);//Display all error messages except E_NOTICE
error_reporting(E_ALL^E_WARNING^E_NOTICE);//Display all error messages except E_WARNING E_NOTICE
error_reporting(E_ERROR | E_WARNING | E_PARSE);//Display runtime errors, which has the same effect as error_reporting(E_ALL ^ E_NOTICE);. error_reporting(E_ALL);//Show all errors
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
Generally, no error will be reported when outputting an undefined declared variable in the default ordinary PHP file, but an error will be reported under the codeigniter framework. This is for "lazy people" who want to integrate adding and modifying pages. It’s very inconvenient. Since I’m a beginner, I still want to block this error message in the code. I even used @, but many people said that @ will greatly reduce the performance...
Finally, I suddenly thought, did codeigniter intentionally prompt this error message? How can we block this type of error? I accidentally searched for "How to prevent codeigniter from displaying Notice information?", and it suddenly dawned on me. It turned out to be this type of error. The error_reporting(E_ALL); in the entry index.php is causing trouble. Just change it to
error_reporting(E_ALL ^ E_NOTICE);
This error can be blocked without affecting other errors.
Here are some information found:
error_reporting() sets PHP’s error reporting level and returns the current level.
Grammar
error_reporting(report_level)
If the parameter level is not specified, the current error level will be returned. The following are possible values for level:
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
2047 E_ALL
2048 E_STRICT
E_NOTICE means that the normal situation is not recorded, and is only used when the program has an error, such as trying to access a non-existent variable, or calling the stat() function to view a non-existent file.
E_WARNING is usually displayed but does not interrupt program execution. This is useful for debugging. For example: calling ereg() with the problematic regular notation.
E_ERROR is usually displayed and will interrupt program execution. This means that memory configuration or other errors cannot be traced using this mask.
E_PARSE Parse errors from syntax.
E_CORE_ERROR Like E_ERROR, but excludes errors caused by the PHP core.
E_CORE_WARNING Like E_WARNING, but does not include PHP core error warnings.
PHP Error Reporting
There are many configuration settings in the php.ini file. You should have already set up your php.ini file and placed it in the appropriate directory, as shown in the documentation for installing PHP and Apache 2 on Linux. There are two configuration variables that you should be aware of when debugging PHP applications. Here are the two variables and their default values:
display_errors = Off
error_reporting = E_ALL
The current default values of these two variables can be found by searching for them in the php.ini file. The purpose of the display_errors variable is obvious - it tells PHP whether to display errors. The default value is Off. However, to make the development process easier, set this value to On:
display_errors = On
The default value of the error_reporting variable is E_ALL. This setting will show everything from bad coding practices to harmless tips to errors. E_ALL is a bit too granular for development purposes, as it also displays hints on the screen for small things (such as variables not being initialized), which messes up the browser's output. I only want to see errors and bad coding practices, but not harmless tips. So, please replace the default value of error_reporting with the following value:
error_reporting = E_ALL & ~E_NOTICE
Restart Apache and everything is set. Next, you'll learn how to do the same thing on Apache.
Error reporting on the server
Depending on what Apache is doing, turning on error reporting in PHP may not work because there may be multiple versions of PHP on the computer. Sometimes it's difficult to tell which PHP version Apache is using because Apache can only look at one php.ini file. Not knowing which php.ini file Apache is using to configure itself is a security issue. However, there is a way to configure PHP variables in Apache so that the correct error level is set.
Furthermore, it is good to know how to set these configuration variables on the server side to override or preempt the php.ini file, thus providing a higher level of security.
When configuring Apache, you should have already touched the basic configuration in the http.conf file in /conf/httpd.conf.
To do what you have already done in the php.ini file, add the following lines to httpd.conf, overwriting any php.ini files:
php_flag display_errors on
php_value error_reporting 2039
This will override the flag already set in the php.ini file for display_errors, as well as the value of error_reporting. The value 2039 represents E_ALL & ~E_NOTICE. If you prefer to use E_ALL, set the value to 2047. Again, you still need to restart Apache.
Next, you need to test error reporting on the server.
Regarding the error_reporting() function, it can shield some error messages, but errors caused by the PHP core cannot be shielded, because errors caused by the PHP core will directly cause the PHP file compilation to fail, because the writing format does not follow the Errors caused by writing PHP coding rules cannot be blocked
Copy the code. The code is as follows:
* For now, avoid warnings of E_STRICT mode
* (this must be done before function definitions)
*/
if (defined('E_STRICT')) {
$old_error_reporting = error_reporting(0);
if ($old_error_reporting & E_STRICT) {
error_reporting($old_error_reporting ^ E_STRICT);
} else {
error_reporting($old_error_reporting);
}
unset($old_error_reporting);
The common ones are as follows:
Copy the code. The code is as follows:
// Turn off all error reporting; turn off all errors
error_reporting(0);
// Report simple running errors; Report a simple running error
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings …); includes reporting some uninitialized variables or catching misspellings of variable names
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini; report all errors except E_NOTICE This is also the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (bitwise 63 may be used in PHP 3); Report all errors
error_reporting(E_ALL);
// Same as error_reporting(E_ALL); Same as above
ini_set('error_reporting', E_ALL);

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software