How to Solve PHP Error Reporting in Programming Language


php error reporting

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

How to Solve PHP Error Reporting

Hypertext Preprocessor ? more commonly known as PHP ? has grown in popularity since it was developed in 1994. Primarily created and designed as a server-side scripting language for web development, it has also grown into a widely-used programming language for general use.

PHP code can be integrated and embedded into HTML (Hypertext Markup Language) programs that make it ideal for web development. Being an open source language, contributors have free reign to add functionality or enhance PHP to suit specific application or business requirements.

Its open source attribute has contributed to the success of PHP, which has been widely distributed across many types of web servers, including nearly any operating system or hardware platform ? all at no charge.

Even with the maturity and evolution of PHP, one factor that continues to plague developers is detecting and reporting PHP errors.

What Are PHP Errors?

When running a PHP script, there are many problems or types of error conditions that could occur:

  • Warnings ? the script noted an issue, but is able to continue
  • Fatal Error ? your script encountered an unrecoverable error, such as failure to connect to a database or locate a file on the server
  • Notice ? informational notification, which may indicate a problem, but could also be acceptable
  • User error ? the user of the page has entered something incorrectly, or perhaps omitted a required field
  • User warning ? you have detected a possible error by the user, but can continue the script
  • User notice ? you advise the user that an issue took place, but the process continued executing

There could be other types of errors that may be applicable to your script, and you are free to develop a framework of error types and actions as appropriate.

As a PHP developer, it?s critical to provide error checking code within your scripts, to avoid frustrating your web site users with these error messages.

When executing your PHP script, you can utilize the standard PHP error handler, or specify that you want to utilize your own customized error handler through the PHP command:

        Set_error_handler(?MyErrorHandler?);

You can place error testing anywhere in your script by including the ?trigger_error? parameter on a test for a field or function. This command could include the level of the error, so you can take the correct action that corresponds to the error.

Now that you know examples of the types of errors, and how to trigger them, the question is how to detect and report the errors.

Logging PHP Error Conditions

For the most part, when PHP errors take place, an error message is sent to the browser indicating the name of the file in use, line number being executed in the PHP script, and a message describing the error encountered.

The PHP default for errors is to write a message to the server?s logging process, or to a specified file. This is defined in the php.ini file on the server, using the error_log configuration setting. This can be altered by using the error_log() function to distribute messages to a file, or to another remote location.

You can also implement code that routes certain error conditions to support personnel or supervisors via email distribution. At your discretion, you can either continue the script, or terminate the process.

By setting the php.ini file setting to display_errors ON, any errors encountered will be logged in the specified destination (file or remote location). For your production systems, you will likely set the value to OFF, so that errors that may contain confidential information are not logged.

On your test or development environments, you will want to have the logging of errors ON, to detect any coding or data errors before your scripts are installed for production use.

PHP reporting setting offers many values for what errors are logged:

  • All ? all warnings and errors
  • Error ? only fatal errors are logged
  • Warning ? run-time warnings are logged
  • Parse ? parsing errors logged at compile time
  • Deprecated ? this is a type of logging for developers to know when a function is used that may be disabled in the future
  • Notice ? notices that indicate to you that either a script error in coding took place, or a script-generated notification such as default variables that were substituted for missing elements
  • Core Error ? PHP initialization fatal errors
  • Core Warning ? PHP initialization warnings
  • Compile Warning ? non-fatal compile warnings
  • User Error ? errors detected from user actions
  • User Warning ? warning messages sent to users
  • User Notice ? notices sent to users due to actions or to advise of results

Each of these types of error conditions can optionally be logged to the file or other destination of your choice, or can be set to not be logged until you designate that the error type should activate logging.

Detecting, Reporting, and Debugging

When you have coded to detect and log errors to the designated log file or other location, the next step is to put this information to use for making your PHP scripts more efficient and responsive for users.

Logging your error conditions with meaningful information will make problem resolution much easier, faster, and effective. Consider the content of the errors you generate from your PHP scripts, so that the proper teams can address the errors for problem resolution.

Populating your error logs with at least these elements will assist with routing details to the person or team positioned to take action:

  • Date/time of the error
  • Script generating the error
  • IP address of the device running the script, and any available credentials
  • Details of the error ? missing element, file not found, etc.
  • Severity of the error ? warning, notice, fatal error

Tracking this information can assist with the development effort and streamline workflow for testing and quality assurance, prior to introducing new or modified scripts to your production environment.

When logging is activated, be sure to consider the impact of how much data is being logged based on your choices. High-volume scripts can generate huge log files that quickly fill up storage capacity on servers, or may impact performance of your applications ? and those of other systems.

Note: keep in mind that PHP logs a great deal of information about error conditions, which may include sensitive information. Security is another reason to limit the number and severity of errors to be logged, and also who has access to logged information.

InMotionHosting advises that log files should be removed or cleaned up periodically, to avoid such issues with system-wide performance and to avoid space issues.

Turning on error logging can be done globally or within your script. Be aware that if your script in turn calls other scripts, the settings could be altered within those scripts. So activating error logs in your code may not help you debug errors in called scripts that have error logging off.

There may be a difference in how you will want errors to display between your development and production servers. To find and trouble-shoot errors during development, you can set errors to display, so that you have immediate notification of the errors. This can be done with the following commands:

  • Error_Reporting = E_ALL
  • Display_errors = On

For production servers, you will want to log errors, but not display them to the screen:

  • Error_Reporting = E_ALL
  • Display_errors = Off
  • Log_Errors = On

PHP Error Reporting

Storing your errors in the error log on your server is only part of problem resolution. Once the data is captured, the challenge is to extract the data and transform it into meaningful information.

Report-writing tools can be helpful for PHP error reporting by extracting the log data and routing data to the right teams or technicians.

Third-party tools such as Rollbar enable you to retrieve and diagnose error conditions quickly and efficiently. Software Development Kits (SDK) are provided by some vendors that allow you to control how the software will interact with your specific script. This can facilitate both the development effort and problem resolution.

Such tools can be a positive alternative to painstakingly working through significant volumes of error logs, making problem resolution, or solving performance issues faster. Many such tools are compatible with PHP applications.

When investigating the use of third-party tools or report-writing programs, be certain that the software is compatible with your programming language and the hardware and operating system utilized by your applications.

Keywords: How to solve PHP error reporting, PHP error reporting

Recent Posts