← Back to PHP Course | Chapter 13: Error Handling | Lesson 1 of 5

PHP Error Handling Introduction

Error handling इस बारे में है कि कुछ गलत होने पर एक program क्या करता है, पहले से planned एक fire drill जैसा। PHP में problems के अलग-अलग levels होते हैं, छोटी notices से लेकर बड़ी stopping errors तक।
Syntax
php
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
<?php
echo $undefinedVar ?? "notice: undefined variable";
echo "\n";
echo 10 / 2; // no error
?>

Error Reporting Level Configure करना

error_reporting() function control करता है कि उन severities में से कौन सी actually display या log होती हैं, आपको development के दौरान सब कुछ दिखाने देते हुए साथ ही production में minor notices silence करते हुए।

उदाहरण: Configuring Error Reporting Level

php
<?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";
?>

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
<?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();
?>

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
<?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;
?>

Clean Error Recovery

restore_error_handler() ज़रूरत न रहने पर PHP के default error handling में वापस चला जाता है, जो तब मायने रखता है जब आप सिर्फ code के एक specific block के लिए custom handling active चाहें।

उदाहरण: Clean Error Recovery

php
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. display_errors on के साथ एक live site पर errors दिखाना, जो visitors को file paths और internal details reveal करता है।
  2. development के दौरान error reporting बंद करना, ताकि real bugs छुप जाएँ।
  3. warnings और notices को ignore करना, जो अक्सर उन bugs की ओर point करते हैं जो बाद में fatal errors बन जाते हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.