Heim  >  Artikel  >  Backend-Entwicklung  >  error_reporting() in PHP

error_reporting() in PHP

王林
王林Original
2024-08-29 13:06:15202Durchsuche

In den verschiedenen Fehlerstufen von PHP ist error_reporting eine Funktion in PHP, die angibt, welche Fehler gemeldet werden, und die error_reporting-Direktive während der Laufzeit bestimmt. Mit dieser Funktion können wir die vorgeschriebene Stufe für die erforderliche Dauer (normalerweise die Laufzeit) unseres Skripts festlegen. Es gibt die alte Fehlermeldestufe basierend auf der eingegebenen Eingabe oder die aktuelle Meldestufe zurück, wenn kein Parameter angegeben ist.

Starten Sie Ihren kostenlosen Softwareentwicklungskurs

Webentwicklung, Programmiersprachen, Softwaretests und andere

Syntax mit Parametern

Es folgt eine Syntax mit Parametern:

Syntax:

error_reporting(level)

Parameter:

Es gibt nur eine einzige Parameterebene, die optional ist und deren Eingabefunktion übernimmt. Es gibt die Fehlerberichtsebene für das aktuelle Skript an. Akzeptierte Werte sind Konstantenname und Wertnummer.

Hinweis: Um die Kompatibilität mit zukünftigen PHP-Versionen sicherzustellen, werden benannte Konstanten empfohlen.

Es gibt einige vordefinierte Konstanten, deren Beschreibung wie folgt lautet:

1. E_Error: Diese weisen auf schwerwiegende Laufzeitfehler hin, die nicht behoben werden können und die Skriptausführung angehalten wird.

2. E_Warning: Dies sind nicht schwerwiegende Fehler, bei denen die Ausführung des Skripts fortgesetzt wird.

3. E_Parse: Hier werden Parsefehler zur Kompilierungszeit angezeigt, die nur von den Parsern generiert werden sollen.

4. E_Notice: Dies gibt Laufzeitbenachrichtigungen aus, die darauf hinweisen, dass das Skript etwas gefunden hat, das einen Fehler anzeigt, der aber auch beim Ausführen eines normalen Skripts auftreten kann.

5. E_Core_Error: Während des ersten Starts von PHP können einige schwerwiegende Fehler auftreten, die vom PHP-Kern generiert werden.

6. E_Core_Warning: Dies zeigt die nicht schwerwiegenden Fehler, die beim ersten Start von PHP auftreten und auch vom PHP-Kern generiert werden.

7. E_Compile_Error: Diese zeigen schwerwiegende Fehler an, die während der Kompilierungszeit auftreten. Diese werden von der Zend-Skript-Engine generiert.

8. E_Compile_Warning: Ähnlich wie oben zeigen diese Warnungen zur Kompilierungszeit an oder können als nicht schwerwiegende Fehler bezeichnet werden und werden auch von der Zend-Skript-Engine generiert.

9. E_User_Error: Hier werden von den Benutzern generierte Fehler angezeigt. Dies ähnelt E_ERROR, außer dass es mithilfe der PHP-Funktion im PHP-Code generiert wird.

10. E_All: Dies ist wie eine Kombination aller oben genannten, die alle Fehler und Warnungen außer E_STRICT unterstützt.

Rückgabewerte:

Die Funktion „error_reporting“ gibt die alte Meldestufe oder die aktuelle Fehlermeldestufe an, falls keine Parameter angegeben sind.

Funktionsweise von error_reporting in PHP

Mit dieser Funktion kann der Entwickler die verschiedenen Arten von Fehlern tatsächlich steuern und festlegen, wie viele solcher Fehler in der Anwendung ausgegeben werden. Diese Funktion legt eine error_reporting-Anweisung fest, die in der PHP-INI-Konfigurationsdatei vorhanden sein wird.

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:

error_reporting() in PHP

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.

Das obige ist der detaillierte Inhalt vonerror_reporting() in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn