← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 2 of 10

PHP Debugging with Xdebug

Xdebug एक tool है जो आपको अपना code pause करने और इसे line by line step through करने देता है, किसी movie पर pause दबाने जैसा। आप हर variable झाँक सकते हैं यह पता लगाने के लिए कि चीज़ें कहाँ गलत हुईं।

Xdebug क्या है?

Xdebug एक PHP extension है जो debugging, profiling, और code-analysis capabilities add करता है जो language में अपने आप नहीं हैं, और serious PHP development के लिए एक standard tool के करीब है।

उदाहरण: What is Xdebug?

php
<?php
// Print `extension_loaded('xdebug') ? "Xdebug is loaded" : "Xdebug not installed"` to the output
echo extension_loaded('xdebug') ? "Xdebug is loaded" : "Xdebug not installed";
// Print "\nAdds debugging, profiling, and code analysis to PHP" to the output
echo "\nAdds debugging, profiling, and code analysis to PHP";
?>

Stack Traces और Var Dumps

Xdebug enable होने पर, PHP का normal error output कहीं richer messages से replace हो जाता है -- एक full stack trace और nicely formatted variable dumps सहित -- जो किसी bug का source ढूँढना काफी तेज़ बना देते हैं।

उदाहरण: Stack Traces and Var Dumps

php
<?php
// Define the function `level3` with no parameters
function level3() { throw new Exception("Something broke"); }
// Define the function `level2` with no parameters
function level2() { level3(); }
// Define the function `level1` with no parameters
function level1() { level2(); }
// Try running this block; jump to `catch` if it throws
try {
    // Call `level1()`
    level1();
// Catch Exception $e
} catch (Exception $e) {
    // Print `$e->getTraceAsString()` to the output
    echo $e->getTraceAsString();
}
?>

Step Debugging

Step debugging आपको अपने IDE में एक breakpoint set करने, script चलाने, और execution को exactly वहीं pause करवाने देता है ताकि आप variables inspect कर सकें और logs से guess करने के बजाय एक-एक करके बाद की lines में step through कर सकें।

उदाहरण: Step Debugging

php
<?php
$x = 5;
$y = 10;
$sum = $x + $y; // set a breakpoint here in your IDE to inspect $x and $y
echo $sum;
?>

Code Coverage

Code coverage tracking record करता है कि एक test run के दौरान कौन सी lines actually execute हुईं, आपकी test suite में gaps reveal करते हुए -- वे code paths जिन्हें कोई test ही नहीं करता।

उदाहरण: Code Coverage

php
<?php
function isEven($n) {
    if ($n % 2 === 0) {
        return true;
    }
    return false; // a test suite might never exercise this line
}
echo isEven(4) ? "even" : "odd";
?>

Performance Profiling

Xdebug का profiler record करता है कि हर function call कितना time और memory consume करती है, एक file produce करते हुए जिसे आप KCachegrind जैसे tool में load करके guess करने के बजाय visually actual bottleneck spot कर सकते हैं।

उदाहरण: Performance Profiling

php
<?php
// Define the function `slowFunction` with no parameters
function slowFunction() {
    for ($i = 0; $i < 100000; $i++) {}
}
// Declare `$start`, set to `microtime(true)`
$start = microtime(true);
// Call `slowFunction()`
slowFunction();
// Print "Took " . round(microtime(true) - $start, 4) . "s -- a profiler shows exactly where time goes" to the output
echo "Took " . round(microtime(true) - $start, 4) . "s -- a profiler shows exactly where time goes";
?>
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 पर Xdebug enabled छोड़ना, जो server को काफी धीमा कर देता है।
  2. IDE port को xdebug.client_port से match करने के लिए set करना भूल जाना, ताकि breakpoints कभी trigger न हों।
  3. stack traces इस्तेमाल करने के बजाय var_dump calls आसपास छोड़ देना।

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.