PHP/Fehlermeldungen: Unterschied zwischen den Versionen

Aus Foxwiki
K Dirkwagner verschob die Seite Programmierung:phpx:ErrorReporting nach Programmierung:PHP:Error Reporting, ohne dabei eine Weiterleitung anzulegen
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
= PHP Turn On or Off Error Reporting =
== Fehlermeldungen an-/ausschalten ==


Error reporting is useful for debugging purposes. As well, perhaps you have some unimportant PHP warning displaying on your website that you would like to remove. This article will show you how to both display and hide PHP errors and warnings from the user.
Das Anzeigen von Fehlern ist fürs Debuggen sehr nützlich. Andersherum will man auch nervige PHP-Warnungen ausblenden. Dieser Artikel zeigt, wie man Fehler anzeigt und Warnungen ausschaltet.


== PHP Code to Display All Errors and Warning Messages ==
=== Code zum Anzeigen aller Fehler und Warnungen ===


To display all warnings and notice this is an easy and simple way that should work in most hosting environments:
Um alle Warnungen und Infos anzuzeigen sollte relativ simpel zu machen sein und in jeder Hostumgebung funktionieren:


  ini_set('display_errors',1);
  ini_set('display_errors',1);
  error_reporting(E_ALL|E_STRICT);
  error_reporting(E_ALL|E_STRICT);


== PHP Code to Turn See Warning Messages and Not Notices ==
=== Code zum Anzeigen der Warnungen ohne Infos ===


  ini_set('display_errors',1);
  ini_set('display_errors',1);
  error_reporting(E_ALL);
  error_reporting(E_ALL);


== PHP Code to Turn Off Error Reporting ==
=== Code zum Abschalten der Fehlermeldungen ===


  error_reporting(0);
  error_reporting(0);
Zeile 24: Zeile 24:
  error_reporting(E_ALL|E_STRICT);
  error_reporting(E_ALL|E_STRICT);


=== Where to Place Code to Remove Errors and Warnings ===
=== Wo sollte der Code platziert werden ===


You can place the bits of code above just about anywhere on in your PHP code but it has to be above the error or else you will not see any warnings or notices since the code is read from the top down.
Die Codestücke können überall im Skript platziert werden. Jedoch sollten sie über dem Fehler platziert werden, da PHP von oben nach unten arbeitet.


=== Tip for WordPress Users: Place Code in Your wp-config.php File ===
=== Tip for WordPress Users: Place Code in Your wp-config.php File ===

Version vom 31. Juli 2020, 13:34 Uhr

Fehlermeldungen an-/ausschalten

Das Anzeigen von Fehlern ist fürs Debuggen sehr nützlich. Andersherum will man auch nervige PHP-Warnungen ausblenden. Dieser Artikel zeigt, wie man Fehler anzeigt und Warnungen ausschaltet.

Code zum Anzeigen aller Fehler und Warnungen

Um alle Warnungen und Infos anzuzeigen sollte relativ simpel zu machen sein und in jeder Hostumgebung funktionieren:

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

Code zum Anzeigen der Warnungen ohne Infos

ini_set('display_errors',1);
error_reporting(E_ALL);

Code zum Abschalten der Fehlermeldungen

error_reporting(0);

or

ini_set('display_errors',0);
error_reporting(E_ALL|E_STRICT);

Wo sollte der Code platziert werden

Die Codestücke können überall im Skript platziert werden. Jedoch sollten sie über dem Fehler platziert werden, da PHP von oben nach unten arbeitet.

Tip for WordPress Users: Place Code in Your wp-config.php File

To turn on or off error reporting site-wide then place the the lines of code above all other PHP in your WordPress site’s wp-config.php file located in your home directory.

PHP Error Reporting Via .htaccess File

If you prefer to use your .htaccess file then you can use the following code to display errors.

php_flag display_errors on
php_value error_reporting 7

Quell: https://impress.org/php-turn-on-or-off-error-reporting/


How to Turn on PHP Error Reporting in MAMP

One of my students asked a question this week about error reporting. When following one of the lessons in the PHP for Beginners course he should have received an error (intended error) but it did not show. He was curious as to why he did not receive the error as he was supposed to.

If you have not set error reporting up within your php.ini file then you will need to edit the file to do this. However, for a beginner, this can be quite daunting. To avoid any unnecessary issues with possible incorrect edits of this important file, you can turn on error reporting within a single script as follows:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);


Line 3: we are just saying that we want all types of errors to be reported

Line 4: this turns on error reporting (the value 1 is on and 0 would be off)

For those who wish to delve into their php.ini file then you will need to look for something along the lines of:

display_errors = on 
error_reporting = E_ALL

I am currently using a Mac environment and have MAMP installed. MAMP actually creates several different php.ini files depending on the version of PHP you are using.

You will find the php.ini files in the following location on MAMP:

Applications > MAMP > conf

If you are running a Windows system with XAMPP then you should find it here:

C:/xampp/php/php.ini

If you are not sure where your php.ini file is located then you can write a simple script in PHP and run it in your browser to see the location of the php.ini file:

<?php
phpinfo();

This will display the following:

I like to use a configuration script so that I can turn on/off error reporting depending on whether I am debugging or not.

<?php
define('DEBUG', true); 
error_reporting(E_ALL); 
if(DEBUG == true){
display_errors(true);
log_errors(false); 
} else {
display_errors(false); 
log_errors(true); 
}

Line 2: you can set the debug mode to true or false as required Line 3: this will report all errors Line 5: test if the debug mode is set to true Line 6: turn on error display Line 7: turn off error logging. It does not make much sense to write the error to a log file if you are reporting to the screen Line 9: turn off display errors Line 10: turn on the writing of errors to the log file

TYPES OF ERROR REPORTING

Within your php.ini file or within the script above you can specify the level of error reporting you require.

E_ALL             - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
E_ERROR           - fatal run-time errors
E_RECOVERABLE_ERROR  - almost fatal run-time errors
E_WARNING         - run-time warnings (non-fatal errors)
E_PARSE           - compile-time parse errors
E_NOTICE          - run-time notices (these are warnings which often result from a bug in your code, but it's possible that it was intentional (e.g., using an uninitialized variable and relying on the fact it is automatically initialized to an empty string)
E_STRICT          - run-time notices, enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code
E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's initial startup
E_COMPILE_ERROR   - fatal compile-time errors
E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
E_USER_ERROR      - user-generated error message
E_USER_WARNING    - user-generated warning message
E_USER_NOTICE     - user-generated notice message
E_DEPRECATED      - warn about code that will not work in future versions of PHP
E_USER_DEPRECATED - user-generated deprecation warnings

Common settings within a php.ini file or the PHP script above include:

E_ALL (Show all errors, warnings and notices including coding standards.)
E_ALL & ~E_NOTICE  (Show all errors, except for notices)
E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)

Quelle: https://www.coding.academy/blog/how-to-turn-on-php-error-reporting-in-mamp