Maximize your Laravel blog's potential with SEO best practices and reach 10K monthly clicks on Google.
Preview the course for free
Benjamin Crozat New!
Benjamin Crozat The art of crafting web applications

PHP & Laravel: how to print an array to see what's hidden

Benjamin Crozat — Updated on

Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting websites!

Powered by

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

PHP & Laravel: how to print an array to see what's hidden



print_r() displays arrays in an human-readable format.


print_r(['Foo', 'Bar', 'Baz']);


[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() prints informations about any type of value. It works great for arrays as well!


var_dump(['Foo', 'Bar', 'Baz']);


array(3) {
string(3) "Foo"
string(3) "Bar"
string(3) "Baz"

You can also print multiple variables at once:

var_dump($foo, $bar, $baz,);


var_export() prints a parsable string representation of a variable. It means you could just copy and paste it into your source code.


$array = ['Foo', 'Bar', 'Baz'];


array (
0 => 'Foo',
1 => 'Bar',
2 => 'Baz',


json_encode() prints can print arrays as JSON.


$array = ['Foo', 'Bar', 'Baz'];
echo json_encode($array);



In Laravel



The dump() function prints in details arrays containing any value.

$array = ['Foo', 'Bar', 'Baz'];

It also accepts an infinity of arguments:

dump($a, $b, $c, $d, $e,);


The dd() function does the same thing as dump(), but stops code execution.

$array = ['Foo', 'Bar', 'Baz'];

It also accepts an infinity of arguments:

dd($a, $b, $c, $d, $e,);

There are multiple ways to check if an array is empty. Let me tell you about each of them and why and when you should use them.

switch, case, and break. What are all these? When should you use it instead of if? What are its pros and cons?

Learn why the "Using $this when not in object context" error happens, and let me show you the only way to fix.

Take your code to the next level, thanks to exceptions. Handle errors in a more graceful way within try and catch blocks.

Store and manage files on Dropbox and use it to back up your Laravel app automatically. Compatible with PHP 8.1+ and Laravel 9+.

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Start leveraging the power of AI today. It enables developers to do incredible things, and many startups build products around it.

Knowing which Laravel version you are running is important before you start writing code on a new project. There are multiple ways to do so.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new feature and change.

Redirects in PHP are simple. I will guide you step by step and show you how to dodge some traps. Finally, we'll learn the nuance between 301 and 302 redirects.

Powered by