← Back to PHP Course | Chapter 2: Output & Input | Lesson 2 of 8

PHP var_dump और print_r

var_dump और print_r आपके variables के लिए x-ray glasses जैसे हैं: वे दिखाते हैं कि असल में अंदर क्या है, इसकी kind और इसका हर part सहित। Programmers इन्हें यह देखने के लिए इस्तेमाल करते हैं कि जब answer अजीब लगे तो क्या गलत हुआ।
Syntax
php
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
<?php
// Declare `$value`, set to "0"
$value = "0";
// Print a detailed dump (with types) of `$value`
var_dump($value);
?>

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
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>

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
<?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;
?>

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
<?php
$data = [
    "user" => ["name" => "Alice", "roles" => ["admin", "editor"]]
];
// Print a detailed dump (with types) of `$data`
var_dump($data);
?>

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
<?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());
?>
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 pages में var_dump() या print_r() calls छोड़ देना, internal data structures को visitors के सामने expose करते हुए।
  2. किसी page पर print_r($arr) call करना और squashed output पढ़ना, जबकि इसे <pre> tags में wrap करना line breaks preserve करता है।
  3. किसी array को सीधे echo करने की कोशिश करना, जो सिर्फ शब्द Array print करता है (एक warning के साथ) इसके contents के बजाय; print_r या var_dump इस्तेमाल करें।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.