PHP var_dump
Summary: in this tutorial, you will learn how to use the PHP var_dump()
function to dump the information about a variable.
Introduction to the 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
:
$balance = 100;
Code language: HTML, XML (xml)
To display the information of the $balance
variable, you place it within parentheses that follow the var_dump
function name like this:
$balance = 100;
var_dump($balance);
Code language: HTML, XML (xml)
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
:
$balance = 100;
$message = 'Insufficient balance';
var_dump($balance);
var_dump($message);
Code language: HTML, XML (xml)
Output:
int(100) string(20) "Insufficient balance"
Code language: JavaScript (javascript)
To make the output more intuitive, you can wrap the output of the var_dump()
function in a pre
tag like this:
$balance = 100;
echo '<pre>';
var_dump($balance);
echo '</pre>';
$message = 'Insufficient balance';
echo '<pre>';
var_dump($message);
echo '</pre>';
Code language: HTML, XML (xml)
Output:
int(100)
string(20) "Insufficient balance"
Code language: JavaScript (javascript)
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 also 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:
function d($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
}
Code language: HTML, XML (xml)
To use the d()
function, you can pass a variable to it as follows:
$balance = 100;
d($amount);$message = 'Insufficient balance';
d($message);
Code language: PHP (php)
Output:
int(100)
string(20) "Insufficient balance"
Code language: JavaScript (javascript)
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);
Code language: PHP (php)
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:
$message = 'Dump and die example';
echo '<pre>';
var_dump($message);
echo '</pre>';
die();
echo 'After calling the die function';
Code language: HTML, XML (xml)
Output:
string(20) "Dump and die example"
Code language: JavaScript (javascript)
How it works
- First, dump the information about the
$message
variable using thevar_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';
Code language: PHP (php)
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:
function dd($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
die();
}
Code language: HTML, XML (xml)
Now, you can use the dd()
function as follows:
// .. dd function$message = 'Dump and die example';
dd($message);
Code language: HTML, XML (xml)
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 apre
tag to make the output more readable. - The die() function terminates the script immediately.
- Combine
var_dump()
anddie()
functions to dump and die.