Making PHP var_dump() values display one line per value

Making PHP var_dump() values display one line per value

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Formatting var_dump() Output for Single-Line Keys

When you call var_dump() on a multi-dimensional array or object in PHP, the default behavior is to output everything on a single, wrapping line if your terminal width accommodates it, or to wrap awkwardly if it exceeds that limit. This often happens when debugging session data, like $_SESSION or complex associative arrays containing multiple string fields. The result is a dense block of text where distinguishing between keys and values becomes difficult as the line grows longer.

Consider a scenario where you are inspecting a user record:

["kt_login_user"]=>  string(8) "teacher1" ["kt_login_id"]=>  string(3) "973" ["kt_campusID"]=>  string(4) "9088" ["kt_positionID"]=>  string(1) "5"

Visually scanning this output is inefficient. You have to mentally parse which string belongs to which key, especially when keys are long or values contain spaces. While var_dump() provides the necessary type information and length hints (like string(8)), its default formatting prioritizes compactness over readability for complex structures.

There is no built-in flag within the var_dump() function itself to force a vertical layout without additional logic. The function outputs directly to the standard output stream, bypassing any standard configuration settings you might find in your PHP environment. To achieve a line-break per value, you must intervene in the output generation process using helper functions or by modifying how the data is printed before it reaches the browser or terminal.

The most straightforward approach involves looping through the array and explicitly formatting each element. You can create a small utility function that iterates over the input variable and appends a newline character after every key-value pair. This manual iteration strips away the default compactness but grants you full control over spacing and indentation.

function dumpVertical($data, $indent = 0) {
    echo str_repeat(' ', $indent);
    
    if (is_array($data)) {
        echo '[';
        foreach ($data as $key => $value) {
            echo "\n" . str_repeat(' ', $indent + 4) . "[$key]=> ";
            
            // Determine type and length for the dump format
            $type = gettype($value);
            if ($type === 'string') {
                echo sprintf("string(%d) '%s'", strlen($value), $value);
            } elseif ($type === 'integer') {
                echo "int(" . $value . ")";
            } elseif ($type === 'boolean') {
                echo gettype($value) === 'boolean' ? (bool)$value : 'boolean';
            } else {
                echo $type;
            }
            
            if (is_array($value)) {
                // Recursively handle nested arrays to maintain structure
                dumpVertical($value, $indent + 4);
            }
            
            $last = end($data);
            if ($key !== array_search($last, $data, true)) {
                echo ',';
            }
        }
        echo "\n" . str_repeat(' ', $indent) . ']';
    } elseif (is_object($data)) {
        echo '{';
        foreach ((array)$data as $key => $value) {
            echo "\n" . str_repeat(' ', $indent + 4) . "[$key]=> ";
            
            $type = gettype($value);
            if ($type === 'string') {
                echo sprintf("string(%d) '%s'", strlen($value), $value);
            } else {
                echo $type;
            }
            
            if (is_array($value)) {
                dumpVertical($value, $indent + 4);
            }
            
            $last = end((array)$data);
            if ($key !== array_search($last, (array)$data, true)) {
                echo ',';
            }
        }
        echo "\n" . str_repeat(' ', $indent) . '}';
    } else {
        echo gettype($data);
        if (is_string($data)) {
            echo sprintf(" (%d)", strlen($data));
        }
    }
}

// Usage
dumpVertical($_SESSION['user']); 

This implementation replicates the internal logic of var_dump regarding type strings and length reporting, but forces a newline after every entry. By manually reconstructing the output string, you ensure that long arrays render vertically, making it significantly easier to spot specific keys like kt_campusID without scrolling through a massive line.

A critical trade-off here is that this method bypasses PHP's internal buffering optimizations found in native var_dump. If you are debugging large datasets in production, printing everything to the screen with excessive newlines can clutter logs or browser output significantly. In such cases, consider directing this formatted output to a log file rather than echoing it directly.

If you prefer not to write custom functions, you might explore third-party debuggers available via Composer. Tools like pestphp/pest often include assertion helpers that format nicely, but for raw var_dump replacement, sticking to the core PHP API usually keeps dependencies low. Alternatively, some environments allow configuring display_errors or using a custom error handler to intercept dumps, though these are more complex setups.

For Laravel developers specifically, if you find yourself constantly needing to inspect session data or model attributes in this vertical format, integrating this logic into your IDE's debug bar or a dedicated debugging package can streamline the workflow without modifying core application code.

Our network