PHP & Laravel: how to print an array to see what's hidden
Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting websites!
There are multiple ways to print the content of an array in PHP.
This article will review each of them built into the language.
Table of contents

In PHP
print_r()
print_r() displays arrays in an human-readable format.
Example:
print_r(['Foo', 'Bar', 'Baz']);
Output:
Array( [0] => Foo [1] => Bar [2] => Baz)
If you need to capture the output, you can pass a second parameter to print_r():
$output = print_r(['Foo', 'Bar', 'Baz'], true);
var_dump()
var_dump() prints informations about any type of value. It works great for arrays as well!
Example:
var_dump(['Foo', 'Bar', 'Baz']);
Output:
array(3) { [0]=> string(3) "Foo" [1]=> string(3) "Bar" [2]=> string(3) "Baz"}
You can also print multiple variables at once:
var_dump($foo, $bar, $baz, …);
var_export()
var_export() prints a parsable string representation of a variable. It means you could just copy and paste it into your source code.
Example:
$array = ['Foo', 'Bar', 'Baz']; var_export($array);
Output:
array ( 0 => 'Foo', 1 => 'Bar', 2 => 'Baz',)
json_encode()
json_encode()
prints can print arrays as JSON.
Example:
$array = ['Foo', 'Bar', 'Baz']; echo json_encode($array);
Output:
["Foo","Bar","Baz"]
In Laravel
dump()
The dump()
function prints in details arrays containing any value.
$array = ['Foo', 'Bar', 'Baz']; dump($array);
It also accepts an infinity of arguments:
dump($a, $b, $c, $d, $e, …);
dd()
The dd()
function does the same thing as dump()
, but stops code execution.
$array = ['Foo', 'Bar', 'Baz']; dd($array);
It also accepts an infinity of arguments:
dd($a, $b, $c, $d, $e, …);