How To Show Php Errors

As a PHP developer, encountering errors is an unavoidable part of the development process. The ability to identify, understand, and resolve these errors is a crucial skill for every developer. In this blog post, we’ll look at the different ways to show PHP errors and how to configure PHP error reporting settings for a smooth debugging experience.

Enabling PHP Error Reporting

By default, PHP may not display all errors, especially on a production server. However, during development, it’s essential to see all errors to quickly identify and fix issues in your code. There are multiple ways to enable error reporting in PHP.

1. Using the php.ini file

The php.ini file is the default configuration file for PHP. You can enable error reporting by modifying the following settings:

display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On

Save the changes and restart your web server for the changes to take effect.

2. Enabling error reporting in your PHP script

You can also enable error reporting directly in your PHP script using the ini_set() function and error_reporting() function. Add the following lines at the beginning of your PHP script:

<p>This will enable error reporting for that specific script.</p>

<h2>Understanding PHP Error Types</h2>

<p>PHP has several error types, each with a specific purpose:</p>

<ul>
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_ERROR:&amp;amp;lt;/strong&amp;amp;gt; A fatal error that causes the <a href="https://teamtutorials.com/other-tutorials/how-to-quit-mail-on-mac">script to terminate</a>.&amp;amp;lt;/li&amp;amp;gt;
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_WARNING:&amp;amp;lt;/strong&amp;amp;gt; A non-fatal error that allows the &amp;lt;a href=&amp;quot;https://teamtutorials.com/other-tutorials/how-to-run-a-command-in-background-in-linux&amp;quot;&amp;gt;script to continue running&amp;lt;/a&amp;gt;.&amp;amp;lt;/li&amp;amp;gt;
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_NOTICE:&amp;amp;lt;/strong&amp;amp;gt; An informational message that suggests a possible issue in the code.&amp;amp;lt;/li&amp;amp;gt;
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_PARSE:&amp;amp;lt;/strong&amp;amp;gt; A parse error caused by malformed code syntax.&amp;amp;lt;/li&amp;amp;gt;
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_DEPRECATED:&amp;amp;lt;/strong&amp;amp;gt; A warning about the use of deprecated functionality.&amp;amp;lt;/li&amp;amp;gt;
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_STRICT:&amp;amp;lt;/strong&amp;amp;gt; A suggestion for improving the code to adhere to best practices.&amp;amp;lt;/li&amp;amp;gt;
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE:&amp;amp;lt;/strong&amp;amp;gt; User-generated error, warning, or notice messages using the &amp;amp;lt;code&amp;amp;gt;trigger_error()&amp;amp;lt;/code&amp;amp;gt; function.&amp;amp;lt;/li&amp;amp;gt;
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_RECOVERABLE_ERROR:&amp;amp;lt;/strong&amp;amp;gt; A catchable fatal error.&amp;amp;lt;/li&amp;amp;gt;
    &amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;E_ALL:&amp;amp;lt;/strong&amp;amp;gt; All error types.&amp;amp;lt;/li&amp;amp;gt;
&amp;amp;lt;/ul&amp;amp;gt;

&amp;amp;lt;h2&amp;amp;gt;Customizing PHP Error Reporting Levels&amp;amp;lt;/h2&amp;amp;gt;

&amp;amp;lt;p&amp;amp;gt;You can customize the level of error reporting to display only specific types of errors. For example, to show only errors and warnings, you can use the following code:&amp;amp;lt;/p&amp;amp;gt;

[sourcecode]
&amp;amp;lt;p&amp;amp;gt;Or, to exclude notices and &amp;lt;a href=&amp;quot;https://teamtutorials.com/other-tutorials/how-to-stop-messages-on-mac-from-iphone&amp;quot;&amp;gt;&lt;a href=&quot;https://teamtutorials.com/other-tutorials/how-to-stop-messages-on-mac-from-iphone&quot;&gt;deprecated messages&lt;/a&gt;&amp;lt;/a&amp;gt;, use the following code:&amp;amp;lt;/p&amp;amp;gt;

[sourcecode]
&amp;amp;lt;h2&amp;amp;gt;Handling PHP Errors with a Custom Error Handler&amp;amp;lt;/h2&amp;amp;gt;

&amp;amp;lt;p&amp;amp;gt;You can create a custom error handler to manage the way errors are displayed or logged. Use the &amp;amp;lt;strong&amp;amp;gt;set_error_handler()&amp;amp;lt;/strong&amp;amp;gt; function to define a custom error handling function. Here&amp;amp;#039;s a simple example:&amp;amp;lt;/p&amp;amp;gt;

[sourcecode]
&amp;amp;lt;br&amp;amp;gt;&amp;amp;quot;;
}

set_error_handler(&amp;amp;quot;custom_error_handler&amp;amp;quot;);

// Trigger an error
echo 10 / 0;
?&amp;amp;amp;gt;

This custom error handler will display a formatted error message with relevant information.

Conclusion

Showing and understanding PHP errors is a vital part of the development process. By configuring error reporting settings and using custom error handlers, you can create a tailored debugging environment that helps you identify and resolve issues in your code more efficiently. Happy coding!