PHP var_dump और print_r
In this page:
var_dump($variable); // type and value
print_r($variable); // readable structure
$text = print_r($variable, true); // return instead of printing
var_dump() क्या है?
var_dump() debugging के लिए बना है, user-facing output के लिए नहीं — यह किसी variable का exact type उसकी value के साथ print करता है, string length और array/object structure सहित।
वह type information exactly वह है जो आपको एक unexpected string "0" को एक falsy value की तरह masquerade करते पकड़ने के लिए चाहिए।
उदाहरण: What is var_dump()?
<?php
// Declare `$value`, set to "0"
$value = "0";
// Print a detailed dump (with types) of `$value`
var_dump($value);
?>
Login to try C/C++/Java/PHP code in the editor
print_r() क्या है?
print_r() completeness से ज़्यादा readability को prefer करता है: यह किसी array या object की structure को एक indented, human-friendly layout में दिखाता है बिना output को type annotations से clutter किए।
यह आमतौर पर पहला tool है जिस तक आप तब पहुँचते हैं जब आपको बस यह देखना हो कि किसी variable के अंदर क्या है।
उदाहरण: What is print_r()?
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
Return Parameter के साथ print_r()
print_r() के दूसरे argument के रूप में true pass करना direct screen output को suppress कर देता है और formatted text को इसके बजाय एक string की तरह return करता है।
यह तब ज़रूरी है जब आप dump को एक log file में लिखना चाहें या इसे तुरंत print करने के बजाय किसी दूसरे string के अंदर embed करना चाहें।
उदाहरण: print_r() with Return Parameter
<?php
// Declare `$data` as an array: `["a" => 1, "b" => 2]`
$data = ["a" => 1, "b" => 2];
// Declare `$output`, set to `print_r($data, true)`
$output = print_r($data, true);
// Call `file_put_contents('log.txt', $output)`
file_put_contents('log.txt', $output);
// Print `$output` to the output
echo $output;
?>
Login to try C/C++/Java/PHP code in the editor
Nested Arrays Debug करना
गहरे nested arrays के लिए — मान लीजिए objects के arrays का एक array — दोनों functions हर level को recursively expand करते हैं, जो exactly वह चीज़ है जो उन्हें complex data inspect करने के लिए एक plain echo से ज़्यादा उपयोगी बनाती है: आप structure का पूरा shape देखते हैं, सिर्फ एक top-level summary नहीं।
उदाहरण: Debugging Nested Arrays
<?php
$data = [
"user" => ["name" => "Alice", "roles" => ["admin", "editor"]]
];
// Print a detailed dump (with types) of `$data`
var_dump($data);
?>
Login to try C/C++/Java/PHP code in the editor
Objects पर var_dump()
किसी object पर var_dump() चलाएँ और आपको इसका class name के साथ हर property की visibility (public/protected/private) और current value मिलती है।
वह visibility detail var_dump() के लिए unique है — print_r() values दिखाता है लेकिन यह नहीं कि हर property का कौन सा access level है।
उदाहरण: var_dump() on Objects
<?php
// Define the class `User`
class User {
// Declare the public property `$name`, set to "Alice"
public $name = "Alice";
// Declare the private property `$password`, set to "secret"
private $password = "secret";
}
// Print a detailed dump (with types) of `new User()`
var_dump(new User());
?>
Login to try C/C++/Java/PHP code in the editor
- production pages में
var_dump()याprint_r()calls छोड़ देना, internal data structures को visitors के सामने expose करते हुए। - किसी page पर
print_r($arr)call करना और squashed output पढ़ना, जबकि इसे<pre>tags में wrap करना line breaks preserve करता है। - किसी array को सीधे
echoकरने की कोशिश करना, जो सिर्फ शब्दArrayprint करता है (एक warning के साथ) इसके contents के बजाय;print_rयाvar_dumpइस्तेमाल करें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: