In the various levels of errors that PHP has, error_reporting is a function in PHP which indicates what are the errors reported and determines the error_reporting directive during runtime. Using this function we can set the prescribed level for the required duration (usually the runtime) of our script. It returns the old error reporting level based on the input given, or the present reporting level when no parameter is given.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax with Parameters
Following is a syntax with parameters:
Syntax:
error_reporting(level)
Parameters:
There is only a single parameter level which is optional and whose input function takes. It specifies the error reporting level for the present script. Accepted values are constant name and value number.
Note: To ensure compatibility for PHP future versions, named constants are recommended.There are a few predefined constants whose description is as below:
1. E_Error: These indicate fatal runtime errors that cannot be recovered from and the script execution will be halted.
2. E_Warning: These are non-fatal errors where the execution of the script will continue.
3. E_Parse: This shows compile-time parse errors which are to be generated only by the parsers.
4. E_Notice: This issues runtime notices indicating that the script has found something which shows an error, but which can also happen while running a normal script.
5. E_Core_Error: During PHP’s initial startup there may arise a few fatal errors which are generated by PHP’s core.
6. E_Core_Warning: This shows the non-fatal errors which arise during PHP’s initial startup also generated by PHP’s core.
7. E_Compile_Error: These display fatal errors which occur during compile time. These are generated by the Zend scripting engine.
8. E_Compile_Warning: Similar to above these display compile-time warnings or can be called non-fatal errors and are also generated by Zend scripting engine.
9. E_User_Error: This displays errors generated by the users. This is similar to E_ERROR except that it is generated using PHP function in the PHP code.
10. E_All: This is like a combination of all the above which supports all errors and warnings except that of E_STRICT.
Return Values:
The error_reporting function gives the old reporting level or the present error reporting level if in case no parameters are given.
Working of error_reporting in PHP
This function allows the developer to actually control the different kinds of errors and how many of such errors will be thrown in the application. This function sets an error_reporting directive that will be present in the PHP ini configuration file.
error_reporting(0);
- When 0 is passed to the error reporting function it removes all warnings, errors, parse related messages and notices, if any. Instead of having to include this line in each of the PHP code files, it is practical to have it added and to turn off these report messages in the ini file present or in the .htaccess.
error_reporting(E_NOTICE);
- In PHP the variables can be used even when not declared. But this practice is not feasible as the undeclared variables may cause application related issues if it is used in conditional statements and loops. This may also take place because of the spelling mismatch between the declared variables and of that being used for conditions and loops. When this E_NOTICE will be passed into the error_reporting function, only then these undeclared variables will be shown in the web application.
error_reporting(E_ALL & ~E_NOTICE);
- This error reporting function helps to filter out the errors which can be displayed. The “~” character here means the “not/no” and hence ~E_NOTICE here means to not show any notices. Here the “&” character represents “true for all” whereas “|” means as long as one of the parameters is true. They are exactly similar to the functions AND and OR in PHP.
error_reporting(E_ALL); error_reporting(-1); ini_set('error_reporting', E_ALL);
- All of the above lines serve the same purpose i.e. show all the errors. E_ALL is the most widely used function among all others by developers to display error messages as it is more comprehensible and intelligible.
Error Logging in PHP using error_log() Function
It happens so that during the production phase, error messages are to be hidden from the end-users but this information is needed to be registered for tracing purpose. And the best way to record these errors on the production web application is to write and store in log files.
An easy way to log these is by using the error_log function which takes our parameters as input. The only mandatory parameter here is the first one which contains details about the errors and what all to be logged. Other parameters like the type, destination, and header are non-mandatory here for this function.
error_log("Error found!", 0);
- The type parameter will be set to 0 by default if not given, and the log information will be appended at the end of the log file generated in the webserver.
error_log("Error information being emailed!", 1, "[email protected]");
- The type parameter here is 1 will email this log specified in the 3rd parameter which is the email id. For this to work, the PHP ini file must be having a correct SMTP configuration to send out emails. Some of the parameters required for these include host, encryption type, port, password and username.
error_log("Write errors to this file", 3, "https://cdn.educba.com/tmp/errorfile.log")<em>;</em>
- The same error logs can also be written down to the required file whose path will be given in the third parameter. Make sure the given path has all required permissions.
Example of error_reporting() in PHP
Given below is the example:
Code:
<?php $a = 1; trigger_error("user warning!", E_USER_WARNING); $a = 2; echo "Value of $a is ${$a}"; error_reporting(0); error_reporting(E_ALL); ?>
Output:
Advantages of using error_reporting function in PHP
- error_reporting is good for debugging purposes and for developing web application.
- Each and every error can be logged and fixed as soon as it happens using this function.
- To not show it to the end-user, make sure you redirect the errors to a log file while releasing it.
Conclusion
Hence we can say that error_reporting() function in PHP are therefore helpful in cases when there are a lot of problems with the PHP web application and we need to display all of these errors and warnings either for development or debugging purposes. It is a function we can enable different kinds of warnings or error messages and most of them are as discussed above.
The above is the detailed content of error_reporting() in PHP. For more information, please follow other related articles on the PHP Chinese website!

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.


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

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 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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 CS6
Visual web development tools
