Get your next remote job on LaraJobs.
1 contributor Edit on GitHub Laravel PHP

Print an array with PHP (+ Laravel)

Table of contents:

Print an array with PHP (+ Laravel)

Introduction to printing arrays in PHP

There are multiple ways to print the content of an array in PHP like var_dump(), print_r(), var_export(), and even json_encode().

Let me review each of them in this article.

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 instead of echoing it, you can pass a second parameter to print_r():

$output = print_r(['Foo', 'Bar', 'Baz'], true);

var_dump() prints information about any type of value. It works great for arrays too!


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


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

You can also print an infinite amount of variables at once:

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

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


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


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

json_encode() can print arrays as JSON.


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



Screenshot of dump() in action.

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

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

And just like var_dump(), it 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,);
Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

console.log() in PHP console.log() in PHP

Explore the world of PHP debugging with var_dump(), and Laravel's friendlier alternatives, dump() and dd(). Much charm, such useful!

Modified on Sep 6, 2023

Easily convert a PHP array to JSON Easily convert a PHP array to JSON

Convert PHP arrays to JSON with `json_encode()`. Ideal for data exchange, storing data, and API communication.

Published on Sep 16, 2023

The fastest way to check if your PHP array is empty The fastest way to check if your PHP array is empty

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.

Modified on Nov 2, 2023 Audio available

Here's the fix to "using $this when not in object context." Here's the fix to "using $this when not in object context."

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

Modified on Dec 14, 2022


Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here


If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -