How to Use PHP var_dump() to Inspect Values and Types

PHP’s var_dump() prints the values and types of one or more expressions. For strings it shows the length in bytes, and for arrays it shows the element count, keys, and values. An integer 1 appears as int(1), so you can tell it apart from the string '1'.

Inspect a value and its type

Pass one or more variables or literal values separated by commas to the PHP manual’s var_dump() function. PHP prints their details in the order you pass them. The function has a void return type. Use an output buffer if you need to capture its output as a string.

Save the first two examples as separate UTF-8 PHP files, then run each by its filename in a terminal, for example php -n types.php. The table lists the first example’s output in order.

<?php
var_dump(1, 1.1, 'hello', 'é', true, null);
Output Meaning
int(1) Integer with value 1
float(1.1) Floating-point number with value 1.1
string(5) "hello" Five-byte string hello
string(2) "é" String é, two bytes in UTF-8
bool(true) Boolean with value true
NULL The null value

The number in string(n) counts bytes. The ASCII string hello occupies five characters and five bytes. In UTF-8, é appears as one character and occupies two bytes.

Read the output of a nested array

Array output starts with array(n) and then shows each key and value with indentation. This example puts a two-element array under the values key of an outer array.

<?php
$data = ['values' => [1, 1.1], 'valid' => true];

var_dump($data);

The outer array(2) contains the keys values and valid. The inner array contains the integer 1 at index 0 and the float 1.1 at index 1.

array(2) {
  ["values"]=>
  array(2) {
    [0]=>
    int(1)
    [1]=>
    float(1.1)
  }
  ["valid"]=>
  bool(true)
}

Keep line breaks when displaying a dump in a browser

Display a dump inside <pre> to preserve indentation and line breaks in HTML. Start buffering with ob_start(), collect the text with ob_get_clean(), and pass it through htmlspecialchars() so a tag inside the data appears as text.

<?php
header('Content-Type: text/html; charset=UTF-8');

$value = ['label' => '<b>sample</b>', 'count' => 2];

ob_start();
var_dump($value);
$dump = ob_get_clean();

echo '<pre>', htmlspecialchars($dump, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), '</pre>';

Save the last block as dump.php in UTF-8. From its folder, run php -n -S 127.0.0.1:8000 and open http://127.0.0.1:8000/dump.php in your browser. Check that the indentation remains and that <b>sample</b> appears literally. Press Ctrl+C in the terminal to stop the server.

Dumps display the actual values. Keep them out of public responses, and use sample data or mask session values, passwords, and authentication tokens while debugging.

Choose between var_dump(), print_r(), and var_export()

Use var_dump() when both the value and its type matter. For a readable array listing you can capture as a string, pass true as the second argument to print_r(). To see a PHP expression for simple values such as scalars and arrays, pass true as the second argument to var_export().

If your terminal output looks different from the example, run PHP with the -n option. This skips the PHP configuration file and helps isolate formatting changes caused by settings or extensions.