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:
<?php
$balance = 100;Code language: PHP (php)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);Code language: PHP (php)If you open the page on the web browser, you’ll see the following output:
int(100)Code language: PHP (php)The output shows the variable’s value (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);Code language: PHP (php)Output:
int(100) string(20) "Insufficient balance"Code language: PHP (php)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>';Code language: PHP (php)Output:
int(100)
string(20) "Insufficient balance"Code language: PHP (php)The output now is much more readable.
The dump helper function #
It’s 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:
<?php
function d($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
}Code language: PHP (php)To use the d() function, you can pass a variable to it as follows:
<?php
function d($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
}
$balance = 100;
d($balance );
$message = 'Insufficient balance';
d($message);Code language: PHP (php)Output:
int(100)
string(20) "Insufficient balance"Code language: PHP (php)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:
<?php
$message = 'Dump and die example';
echo '<pre>';
var_dump($message);
echo '</pre>';
die();
echo 'After calling the die function';Code language: PHP (php)Output:
string(20) "Dump and die example"Code language: PHP (php)How it works
- First, dump the information about the
$messagevariable 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:
<?php
function dd($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
die();
}Code language: PHP (php)Now, you can use the dd() function as follows:
<?php
function dd($data)
{
echo '<pre>';
var_dump($data);
echo '</pre>';
die();
}
$message = 'Dump and die example';
dd($message);Code language: PHP (php)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 apretag to make the output more readable. - The die() function terminates the script immediately.
- Combine
var_dump()anddie()functions to dump and die.