Laravel 5: Displaying Variable Data and Stopping Script Execution for Debugging With dd

April 15, 2018 —John Koster

The dd function is a debug utility that will display useful information about the objects or values supplied. This function will also prevent further execution of the script; it will dump the values of the supplied arguments and die (exit the script). In some ways, this function is a more elaborate version of PHP's var_dump function.

The dd function will also take into account if the current script is running within the console (CLI) or if it is returning a response to a web client, such as a browser; it will update its output accordingly.

#Signature

The signature of the dd function is:

1function dd(
2 ...$args
3);

#Example Use

The uses of the dd function are vast, and you will undoubtedly come up with your own great uses. However, for the following example, let's create assume we have the following values available to use:

1<?php
2 
3$arrayValue = [
4 'firstName' => 'John',
5 'lastName' => 'Doe'
6];
7 
8$objectValue = (object)$arrayValue;

What we have done is created an array with some sample data and then cast it into an instance of stdClass (via the object) cast. If we were to invoke the dd function like so:

1dd($arrayValue, $objectValue);

We would see output similar to the following in a web browser:

1array:2 [
2 "firstName" => "John"
3 "lastName" => "Doe"
4]
5{#248 ▼
6 +"firstName": "John"
7 +"lastName": "Doe"
8}

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.