PHP Error Handling Introduction
In this page:
error_reporting(E_ALL);
trigger_error("message", E_USER_WARNING); // or E_USER_NOTICE, E_USER_ERROR
PHP में Errors के Types
PHP कई error severities को अलग करता है: notices minor issues flag करती हैं जैसे एक undefined variable, warnings execution को halt किए बिना बड़ी problems report करती हैं, और fatal errors script को तुरंत रोक देते हैं -- कौन सा कौन सा है यह जानना shape करता है कि आपको कितनी urgently react करना है।
उदाहरण: Types of Errors in PHP
<?php
echo $undefinedVar ?? "notice: undefined variable";
echo "\n";
echo 10 / 2; // no error
?>
Login to try C/C++/Java/PHP code in the editor
Error Reporting Level Configure करना
error_reporting() function control करता है कि उन severities में से कौन सी actually display या log होती हैं, आपको development के दौरान सब कुछ दिखाने देते हुए साथ ही production में minor notices silence करते हुए।
उदाहरण: Configuring Error Reporting Level
<?php
// Call `error_reporting(E_ALL)`
error_reporting(E_ALL);
// Print "All errors, warnings, and notices will now be shown" to the output
echo "All errors, warnings, and notices will now be shown";
?>
Login to try C/C++/Java/PHP code in the editor
Custom Errors Trigger करना
trigger_error() आपके अपने code को demand पर एक warning या notice raise करने देता है, जो PHP अपने खुद के issues को flag करने के same तरीके suspicious application-level conditions (जैसे एक deprecated function call) को flag करने के लिए उपयोगी है।
उदाहरण: Triggering Custom Errors
<?php
// Define the function `useOldFunction` with no parameters
function useOldFunction() {
// Call `trigger_error("This function is deprecated", E_USER_WARNING)`
trigger_error("This function is deprecated", E_USER_WARNING);
}
// Call `useOldFunction()`
useOldFunction();
?>
Login to try C/C++/Java/PHP code in the editor
Custom Error Handlers
set_error_handler() आपको एक custom function register करने देता है जो PHP के built-in errors को intercept करता है, ताकि आप उन्हें log कर सकें, consistently format कर सकें, या end users से raw error details छुपा सकें।
उदाहरण: Custom Error Handlers
<?php
set_error_handler(function ($errno, $errstr) {
// Print "Custom handler caught: $errstr" to the output
echo "Custom handler caught: $errstr";
});
// Print `$undefinedVar` to the output
echo $undefinedVar;
?>
Login to try C/C++/Java/PHP code in the editor
Clean Error Recovery
restore_error_handler() ज़रूरत न रहने पर PHP के default error handling में वापस चला जाता है, जो तब मायने रखता है जब आप सिर्फ code के एक specific block के लिए custom handling active चाहें।
उदाहरण: Clean Error Recovery
<?php
set_error_handler(function ($errno, $errstr) {
// Print "Custom: $errstr\n" to the output
echo "Custom: $errstr\n";
});
// Call `restore_error_handler()`
restore_error_handler();
// Print "Back to PHP's default error handling" to the output
echo "Back to PHP's default error handling";
?>
Login to try C/C++/Java/PHP code in the editor
display_errorson के साथ एक live site पर errors दिखाना, जो visitors को file paths और internal details reveal करता है।- development के दौरान error reporting बंद करना, ताकि real bugs छुप जाएँ।
- warnings और notices को ignore करना, जो अक्सर उन bugs की ओर point करते हैं जो बाद में fatal errors बन जाते हैं।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: