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

PHP Debugging Techniques

Debugging एक detective होना है जो यह ढूँढता है कि code misbehave क्यों कर रहा है। आप values print out करते हैं, messages पढ़ते हैं, और culprit मिलने तक clues देखते हैं।
Syntax
php
var_dump($variable);
print_r($variable);
debug_backtrace();

if (DEBUG_MODE) {
    var_dump($variable);
}

Simple Print Debugging

var_dump() और print_r() किसी variable के actual contents inspect करने का सबसे तेज़ तरीका हैं -- var_dump() इसके अलावा data types और array/object structure भी reveal करता है, जो तब मायने रखता है जब कोई bug असल में एक type mismatch निकले न कि एक गलत value।

उदाहरण: Simple Print Debugging

php
<?php
// Declare `$data` as an array: `["id" => 1, "active" => true]`
$data = ["id" => 1, "active" => true];
// Print a detailed dump (with types) of `$data`
var_dump($data);
// Print a human-readable dump of `$data`
print_r($data);
?>

Backtrace Utilities

debug_backtrace() function calls की पूरी chain return करता है जो current line तक ले गई, जो तब invaluable है जब आप किसी गहरे nested call के अंदर पहुँच जाएँ और यह समझने की ज़रूरत हो कि execution वहाँ कैसे पहुँची।

उदाहरण: The Backtrace Utilities

php
<?php
// Define the function `a` with no parameters
function a() { b(); }
// Define the function `b` with no parameters
function b() { print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)); }
// Call `a()`
a();
?>

Conditional Debugging

debug output को DEBUG_MODE जैसे constant के against एक check में wrap करना आपको production में इसे बिना कभी चलाए (या information leak किए) diagnostic code जगह पर छोड़ने देता है।

उदाहरण: Conditional Debugging

php
<?php
// Call `define('DEBUG_MODE', true)`
define('DEBUG_MODE', true);
// Check whether `DEBUG_MODE`
if (DEBUG_MODE) {
    // Print "Debug: variable value is 42" to the output
    echo "Debug: variable value is 42";
}
?>

Memory और Time Track करना

memory_get_usage() और microtime(true) आपको exactly measure करने देते हैं कि एक script कितनी memory consume करती है और एक section चलने में कितना समय लेता है, vague 'this feels slow' complaints को concrete numbers में बदलते हुए जिनके against आप optimize कर सकें।

उदाहरण: Tracking Memory and Time

php
<?php
// Declare `$start`, set to `microtime(true)`
$start = microtime(true);
// Declare `$mem`, set to `memory_get_usage()`
$mem = memory_get_usage();
for ($i = 0; $i < 10000; $i++) {}
// Print "Time: " . round(microtime(true) - $start, 4) . "s\n" to the output
echo "Time: " . round(microtime(true) - $start, 4) . "s\n";
// Print "Memory: " . (memory_get_usage() - $mem) . " bytes" to the output
echo "Memory: " . (memory_get_usage() - $mem) . " bytes";
?>

Safe Variable Inspection

var_export() किसी variable की value represent करने वाला valid PHP code का एक string produce करता है, जो cache files या config arrays generate करने के लिए handy है, सिर्फ debugging के दौरान एक value देखने के लिए नहीं।

उदाहरण: Safe Variable Inspection

php
<?php
// Declare `$config` as an array: `["theme" => "dark", "retries" => 3]`
$config = ["theme" => "dark", "retries" => 3];
// Print `var_export($config, true)` to the output
echo var_export($config, true);
?>
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. production में जाने वाले code में var_dump() या print_r() calls छोड़ना, जो visitors को internal data दिखाता है।
  2. header() calls से पहले debugging output, जो एक headers already sent warning trigger करता है।
  3. browser में print_r output के आसपास <pre> भूल जाना, ताकि layout unreadable हो।
चैप्टर सारांश
  • Error handling PHP के errors समझने से शुरू होती है, और try-catch exceptions handle करता है।
  • Custom exceptions specific error types बनाते हैं।
  • Error logging और debugging techniques आपको problems ढूँढने में मदद करती हैं।
🔒

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.