PHP Debugging Techniques
In this page:
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
Backtrace Utilities
debug_backtrace() function calls की पूरी chain return करता है जो current line तक ले गई, जो तब invaluable है जब आप किसी गहरे nested call के अंदर पहुँच जाएँ और यह समझने की ज़रूरत हो कि execution वहाँ कैसे पहुँची।
उदाहरण: The Backtrace Utilities
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
Conditional Debugging
debug output को DEBUG_MODE जैसे constant के against एक check में wrap करना आपको production में इसे बिना कभी चलाए (या information leak किए) diagnostic code जगह पर छोड़ने देता है।
उदाहरण: Conditional Debugging
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- production में जाने वाले code में
var_dump()याprint_r()calls छोड़ना, जो visitors को internal data दिखाता है। header()calls से पहले debugging output, जो एक headers already sent warning trigger करता है।- browser में
print_routput के आसपास<pre>भूल जाना, ताकि layout unreadable हो।
- Error handling PHP के errors समझने से शुरू होती है, और
try-catchexceptions 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: