PHP var_dump function
The var_dump() is a built-in function that allows you to dump the information about a
variable. The var_dump() function accepts a variable and displays its type and value.
Suppose that you have a variable called $balance with a value of 100:
<?php
$balance = 100;
To display the information of the $balance variable, you place it within parentheses that
follow the var_dump function name like this:
<?php
$balance = 100;
var_dump($balance);
If you open the page on the web browser, you’ll see the following output:
int(100)
The output shows the value of the variable (100) and its type (int) which stands for integer.
The following shows how to dump information about two variables $amount and $message:
<?php
$balance = 100;
$message = 'Insufficient balance';
var_dump($balance);
var_dump($message);
Output:
int(100) string(20) "Insufficient balance"
To make the output more intuitive, you can wrap the output of the var_dump() function in a
pre tag like this:
<?php
$balance = 100;
echo '<pre>';
var_dump($balance);
echo '</pre>';
$message = 'Insufficient balance';
echo '<pre>';
var_dump($message);
echo '</pre>';
Output:
int(100)
string(20) "Insufficient balance"
The output now is much more readable.
The dump helper function
It’s kind of tedious to always echo the opening <pre> and closing </pre> tags when you dump
the information about the variable.
To make it easier, you can define a function and reuse it. For now, you can think that a
function is a reusable piece of code that can be referenced by a name. A function may have
input and output.
PHP has many built-in functions like var_dump(). It also allows you to define your own
functions. These functions are called user-defined functions. And you’ll learn more about it
in the function tutorial.
The following defines a function called d() that accepts a variable. It shows the information
about the variable and wraps the output in the <pre> tag:
<?php
function d($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
}
To use the d() function, you can pass a variable to it as follows:
$balance = 100;
d($amount);
$message = 'Insufficient balance';
d($message);
Output:
int(100)
string(20) "Insufficient balance"
The output is much cleaner now.
Dump and die using the var_dump() and die() functions
The die() function displays a message and terminates the execution of the script:
die($status);
Sometimes, you want to dump the information of a variable and terminate the script
immediately. In this case, you can combine the var_dump() function with the die() function
as follows:
<?php
$message = 'Dump and die example';
echo '<pre>';
var_dump($message);
echo '</pre>';
die();
echo 'After calling the die function';
Output:
string(20) "Dump and die example"
How it works
First, dump the information about the $message variable using the var_dump() function.
Second, terminate the script immediately by calling the die() function.
Since the die() function terminates the script immediately, the following statement did not
execute:
echo 'After calling the die function';
Therefore, you didn’t see the message in the output.
To make the code reusable, you can wrap the code snippet above in a function e.g., dd(). The
name dd stands for the dump and die:
<?php
function dd($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
die();
}
Now, you can use the dd() function as follows:
<?php
// .. dd function
$message = 'Dump and die example';
dd($message);
In the later tutorial, you will learn how to place the functions in a file and reuse them in any
script.
Summary
• Use the var_dump() function to dump the information about a variable.
• Wrap the output of the var_dump() function in a pre tag to make the output more
readable.
• The die() function terminates the script immediately.
• Combine var_dump() and die() functions to dump and die.