How to enable or disable error display in PHP? Previous error messages
An integral part of programming is identifying errors in the code. Even if a programmer is a brilliant programmer, he will still make mistakes, sometimes even trivial ones. This is facilitated by human nature itself. Therefore, the programmer spends quite a large part of his time debugging and identifying errors in the code. Accordingly, the more flexible and convenient the tools for identifying errors in code are, and the better the programmer knows them, the higher his productivity.
Since PHP is script language programming, then all errors made in the code are identified as the code is executed. A PHP programmer will have to deal with both standard errors inherent in programming in general, and rather hidden errors - such as typos in variable naming.
The PHP language provides two mechanisms for detecting errors in a script during its execution: the standard PHP error mechanism and the exception mechanism. In PHP, a lot of functionality uses the standard error mechanism, so it’s impossible not to know about it. Therefore, this article will focus on this mechanism.
The standard PHP error mechanism is quite simple - if the PHP interpreter encounters an error while executing a script, it tries to notify the programmer about it. However, there are quite a lot of settings for this mechanism, so it is quite difficult for a programmer who knows PHP to understand them.
We see errors
So, in order for PHP to display errors directly on the page, you need to set a special parameter in the code:
ini_set("display_errors", 1); //display_errors is responsible for displaying errors directly on the page. If 0 – errors are not displayed
Now, if any error occurs in the script, the interpreter will display information about it on the page. For example, if you run a script with a non-existent function:
ini_set("display_errors", 1);
echo "
String to be displayed
";echo abrakadabra();
echo "
A line that will not be displayed due to an error
";then the following error will be displayed on the page:
Fatal error. This kind of error means that the script is interrupted and the following code will not be executed.
Displaying script errors on the page is only used to debug the script itself, so when running the code in production, the display_errors parameter is set to 0.
In general, in PHP there are errors of different categories of significance: some of them interrupt further execution of the code - others do not, some can be intercepted - others cannot. A complete list of error categories is given below:
Meaning | Constant | Description | Opportunity |
1 | E_ERROR | Fatal error | No |
2 | E_WARNING | Correctable error | Yes |
4 | E_PARSE | Parser error | No |
8 | E_NOTICE | Potential error | Yes |
16 | E_CORE_ERROR | Similar to E_ERROR, but generated by the PHP core | No |
32 | E_CORE_WARNING | Similar to E_WARNING, but generated by the PHP core | No |
64 | E_COMPILE_ERROR | Similar to E_ERROR, but generated by Zend Engine | No |
128 | E_COMPILE_WARNING | Similar to E_WARNING, but generated by Zend Engine | No |
256 | E_USER_ERROR | Similar to E_ERROR, but triggered by calling trigger_error() | Yes |
512 | E_USER_WARNING | Similar to E_WARNING, but triggered by calling trigger_error() | Yes |
1024 | E_USER_NOTICE | Similar to E_NOTICE, but triggered by calling trigger_error() | Yes |
2048 | E_STRICT | Message from the runtime with recommendations for improving code quality (starting with PHP5) | - |
4096 | E_RECOVERABLE_ERROR | Dangerous but not fatal error (for example, type mismatch) | Yes |
8192 | E_DEPRECATED | Deprecated feature or functionality warning | Yes |
16384 | E_USER_DEPRECATED | Deprecated function or feature warning raised in code | Yes |
32767 | E_ALL | All errors | No |
For convenience, constants are provided that are used to determine the level of error processing and construct a bit mask. Constants have meaningful names. Looking at the constant - we can say that the E_PARSE level error occurs in case of a syntax error, E_NOTICE is a reminder to the programmer about the violation of the "good style" of PHP programming.
By default, the error message generation mode is E_ALL & ~E_NOTICE, which corresponds to the output of all messages that are not classified as E_NOTICE. The programmer can flexibly configure which categories of errors he needs to see on the page. In order to change the error generation mode, you need to change the error_reporting configuration parameter. To enable the display of any errors, you must set this parameter E_ALL value:
For example, if you run the following code:
ini_set("display_errors", 1);
ini_set("error_reporting", E_ALL);
We will receive errors of two categories at once (notice and warning):
When an undeclared variable is used, an E_NOTICE error appears. When connecting to the base MySQL data(or other) fails - the PHP interpreter reports an E_WARNING error.
Logging errors
Even if the display_errors configuration parameter is set to 0, it should still be possible to view whether any errors occurred during script execution. Thus, if an error occurs, we must know where and why it happened. For these purposes, PHP has error logging settings.
By default, errors are not logged anywhere using the PHP language. To change this, you need to change the log_errors configuration parameter to 1 from 0:
ini_set("log_errors", 1);
In this case, errors will be automatically written to the file specified in the error_log configuration parameter. The error_log parameter can also be changed by specifying the path to the desired file where errors will be logged:
ini_set("error_log", "/var/log/php_errors.log");
So, for the code:
ini_set("display_errors", 1);
ini_set("error_reporting", E_ALL);
ini_set("log_errors", 1);
ini_set("error_log", __DIR__ . "/log.txt");
$db = mysql_connect($db_host, "user", "password");
Similar information is recorded in the log file and displayed on the page:
PHP Notice: Undefined variable: db_host in Z:\home\test\www\index.php on line 7
PHP Warning: mysql_connect(): Access denied for user "user"@"localhost" (using password: YES) in Z:\home\test\www\index.php on line 7
Handling errors
Errors are divided into catchable and non-catchable. Non-fatal errors can be caught by user code. The error category description summary table reveals which error categories can be caught.
In order to catch non-critical errors in code, it is enough to implement and declare own function and pass its name to the set_error_handler function. At the same time, in implemented function 5 parameters are passed:
- error level as an integer;
- error message as a string;
- the name of the file in which the error occurred, as a string;
- the number of the line in which the error occurred, as an integer;
- an array of all variables that exist in the scope where the error occurred. Moreover, global variables will also be included.
Conclusion
PHP has powerful tools for handling and managing standard error propagation. Therefore, catching this or that error will not be difficult, provided that the script is correctly configured. Knowledge of this area is integral to any PHP programmer, since a lot of code uses the mechanism described above
I often heard about this problem from other users. Some, because of the hoster, need to hide errors that appear, others, on the contrary, need to understand what is happening with their code, because not a single error is shown. In this article I will try to show all the main ways to display/hide errors.
There is a deliberate error in the example, but it will NOT be displayed
$value = @$var[$key];
2) You can also insert a setting for the error display parameter (display_errors) before the PHP script being checked. It can take on the value either On (show) or Off (hide).
Ini_set("display_errors","On");
error_reporting("E_ALL");
And accordingly, after the code that was checked for errors, set the parameter back.
Ini_set("display_errors","Off");
For example, you want to see errors in the script
Ini_set("display_errors", "On"); // error messages will be shown
error_reporting(E_ALL); // E_ALL - display ALL errors
$value = $var[$key]; // error example
ini_set("display_errors", "Off"); // now there will be NO messages
You can set it the other way around (off in the top and on in the bottom) so that errors are NOT displayed in a specific section of the code.
Php_flag display_errors On
#show all errors except warnings (Notice)
php_value error_reporting "E_ALL & ~E_NOTICE"
In php.ini:
Error_reporting = E_ALL
display_errors On
IN top line We select all types of errors, and at the bottom we give the go-ahead for their display.
After the edits, you need to restart Apache for the settings to be changed and take effect (graceful or restart):
Sudo apachectl -k graceful
In what order is the error parameter processed? At the very beginning, the php.ini parameter is taken into account, then .htaccess, and then what is specified directly in the PHP script. So if something doesn’t work, then look up the chain, perhaps there is a different setting there.As always, thank you for your attention and good luck! I hope the article was useful!
I'm trying to parse HTML5 code so I can set attributes/values in code, but it seems DOMDocument (PHP5.3) doesn't support tags like and .
Is there a way to parse this as HTML in PHP and manipulate the code?
Code to play: