Updated April 4, 2023
Introduction to PHP display_errors
PHP display_errors have some function that is used to define the list of errors and warnings with appropriate and detailed information. PHP display_errors provide aid to developers concerning troubleshooting and debugging. Any application related to PHP gets halted and stopped due to some reason will include some set of errors or warning that needs to be displayed as part of PHP display_errors. There are different levels of errors involved as part of display_errors at the time of execution. PHP Display_error order mentioned in the document should always be in “ON” mode when present within the document.
Syntax
There is no proper syntax for PHP display_errors, but still, some set of lines of codes with a specific format needs to be included within the script at the time of execution of PHP code at the time of execution. The line of codes is represented as follows :
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
where,
- ini_set(): This is the function that mainly will try to override the configuration found initially in the ini file.
- display_errors: It is one of the directives that will determine whether there is a need to determine the error or remain hidden from the end-user.
- display_startup_errors: It is another directive that is also behaving similarly as display_errors with a difference in the fact that the display startup error will be responsible for fetching all the errors at the startup time the screen.
- error_reporting(): Error reporting function acts like a filter which is mostly responsible for filtering out the errors necessary for display.
- E-ALL: It is the most recommended directive by developers as it provides more understandable and readable formats to developers.
How display_errors work in PHP?
- PHP has a good significance for display_errors as it gives the programmers the ability to troubleshoot and provide a proper readable and understandable format of code.
- It lets programmers debug with the detailed set of information concerning errors and warnings it provides.
- The easiest way to achieve the display of all kinds of errors in PHP is to include some set of statements or codes within the ini file at the time of execution and compilation.
- It helps in achieving the detailed display of error while execution by including some lines of code within the ini file as mentioned in the syntax.
- Each function with the arguments passed on to the function has its own significance.
- Ini() function always try to override the current configuration present within the ini file of PHP, and two directives make use of the ini function religiously. Both the two directives have their own role.
- Display error directive is used whenever the need to display the error is not mandatory, and at the time of development, the display_error directive should be kept off; otherwise, it will create a hurdle.
- On the contrary, display_startup_error has a different significance in the sense it is used only when the code execution gets started with the startup sequence as the Display error directive never participates for the same. It always makes use of the ini function to provide total functionality.
- Also, some more interesting characteristics are involved related to both the directives; that is, they won’t be able to display any kind of Parse errors. Suppose in case the programmers forget to put the semicolon or some misses to put the curly braces than in that case it will be a problem for programmers as mentioned with the reason.
- Sometimes it happens that in the real-time environment or testing environment, it becomes difficult to test with the already mentioned directives; then, in that case, there is the usage of some additional directives that need to be made either on or off to handle the implementation of code with the browser.
- A solution to the above scenario is to make the display_error directive as ON using the following command to be included in the ini file as display_error = on. Then restart the apache tomcat folder by restoring the ini file in a working folder of apache tomcat.
- This way, the ini_set() function will incorporate all the major directives as parameters and pass them accordingly at the time of execution of code.
Examples
Here are the following examples mention below
Example #1
This program demonstrates the inclusion of a set of code within the ini file, but it fails as the display error and display startup error does not include parse errors depicting the exact location of the failure within the file and the output as shown.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
ini_set('display_errors', 1);
ini_set('display_start_up_errors', 1);
error_reporting(E_ALL);
include("abcd.php");
?>
</body>
</html>
Output:
Note: To get the above error displayed in the proper understandable and readable format, then it is very much needed to display errors, including parse errors for which it is needed to make a change in php.ini or pgp-fpm in apache tomcat, i.e. in the working folder of apache tomcat with the following command as part of the configuration file: display_errors = ON.
Example #2
This program demonstrates other types of errors that can be displayed, not necessarily including the overall PHP display_error functions. But sometimes, these kinds of errors or warnings come up with the developers as execution is almost smooth and should get easily solved.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
$z = "Representation_of_Notice_Error.";
echo $z;
echo $Notice;
?>
</body>
</html>
Output:
Example #3
This program demonstrates that the display_error with ini() function is enabled and restarted, as shown in the output. This warning comes into the picture when the display_error is enabled and restarted in the apache tomcat working folder.
Code:
<!DOCTYPE html>
<html>
<body>
<?php
for($h_1 = 1; $h_1 <= 8 $h_1++)
{
echo $h_1;
}
?>
</body>
</html>
Output:
Conclusion
PHP display_error is quite an appropriate functionality as part of PHP and provides aid to programmers by providing them with the ability and flexibility to troubleshoot and debug the application easily. It makes the time consumption of debugging and troubleshooting less. Overall, It is a very efficient approach to use display_errors in PHP to get detailed information for errors and warnings as it helps in many ways.
Recommended Articles
This is a guide to PHP display_errors. Here we discuss the introduction, syntax, and working of display_errors in PHP along with different examples. You may also have a look at the following articles to learn more –