🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

PHP – var_dump

Master PHP's var_dump() to inspect any variable's type and value. Compare it to print_r() and var_export() for effective debugging.

PHP var_dump()

As a PHP developer, you will eventually hit a wall where your code breaks, your loops crash, or your database refuses to save user information. In these frustrating moments, you need to literally look inside the brain of the PHP framework to see precisely what data a variable holds.

Your single best friend in the entire language for this process is var_dump().

What is var_dump()?

The var_dump() function natively “dumps” all known information about one or more variables directly into the browser.

Unlike echo or print, which only generate standard output strings, var_dump() reveals the absolute granular truth regarding the structural integrity of your elements, explicitly detailing:

  1. The structured Data Type (String, Integer, Boolean, Array, Object)
  2. The exact Length/Size of the active entity
  3. The genuine, unformatted Value

Basic Implementation

Observe what happens when we use $username = "CodesCompiler" and suddenly process it identically through both echo and var_dump().

<?php
  // Initialize test data
  $username = "CodesCompiler";
  $age = 29;
  $isLoggedIn = true;

  // Standard echo statements
  echo $username;      // Displays "CodesCompiler"
  echo $age;           // Displays "29"
  echo $isLoggedIn;    // Displays "1"

  echo "<hr>";

  // Dumping the true variable states
  var_dump($username);   // Displays: string(13) "CodesCompiler"
  var_dump($age);        // Displays: int(29)
  var_dump($isLoggedIn); // Displays: bool(true)
?>

Debugging Arrays

While var_dump() is incredibly helpful for analyzing raw strings and booleans, its true superpower is recursively reading complex, multi-tiered data grids. If you try to run echo on an Array, you will merely get an enormous Array to string conversion Error!.

var_dump() handles arrays effortlessly!

<?php
  $shoppingList = array("Apples", "Bananas", "Milk");
  
  var_dump($shoppingList);
  
  /* 
    The engine explicitly outputs:
    array(3) {
      [0]=> string(6) "Apples"
      [1]=> string(7) "Bananas"
      [2]=> string(4) "Milk"
    }
  */
?>

Pro-Tip for Reading Dumps!

[!TIP] When executing var_dump() onto a standard webpage, the browser attempts to format it natively. To maintain strict formatting spaces and visual elegance, securely wrap your var_dump inside HTML <pre> tags!

Interactive Sandbox

You can test how deeply var_dump() operates dynamically below. Try altering the nested categories inside the $_SERVER_MOCK array and see how the interpreter logically prints the strict byte size of your custom strings!

Preview