PHP Debugging with Xdebug
In this page:
Xdebug क्या है?
Xdebug एक PHP extension है जो debugging, profiling, और code-analysis capabilities add करता है जो language में अपने आप नहीं हैं, और serious PHP development के लिए एक standard tool के करीब है।
उदाहरण: What is Xdebug?
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
}
?>
Login to try C/C++/Java/PHP code in the editor
Step Debugging
Step debugging आपको अपने IDE में एक breakpoint set करने, script चलाने, और execution को exactly वहीं pause करवाने देता है ताकि आप variables inspect कर सकें और logs से guess करने के बजाय एक-एक करके बाद की lines में step through कर सकें।
उदाहरण: Step Debugging
<?php
$x = 5;
$y = 10;
$sum = $x + $y; // set a breakpoint here in your IDE to inspect $x and $y
echo $sum;
?>
Login to try C/C++/Java/PHP code in the editor
Code Coverage
Code coverage tracking record करता है कि एक test run के दौरान कौन सी lines actually execute हुईं, आपकी test suite में gaps reveal करते हुए -- वे code paths जिन्हें कोई test ही नहीं करता।
उदाहरण: Code Coverage
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
Performance Profiling
Xdebug का profiler record करता है कि हर function call कितना time और memory consume करती है, एक file produce करते हुए जिसे आप KCachegrind जैसे tool में load करके guess करने के बजाय visually actual bottleneck spot कर सकते हैं।
उदाहरण: Performance Profiling
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
- production पर Xdebug enabled छोड़ना, जो server को काफी धीमा कर देता है।
- IDE port को
xdebug.client_portसे match करने के लिए set करना भूल जाना, ताकि breakpoints कभी trigger न हों। - stack traces इस्तेमाल करने के बजाय
var_dumpcalls आसपास छोड़ देना।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: