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
Artboard

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

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()

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,);
Recommended

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