HOWTO · PHP

How to Echo or Print an Array in PHP

This article introduces how to echo or print an array in PHP. It includes foreach loop, print_r() function and var_dump() function.

On this page

An array cannot be passed directly to echo as a readable list of its values. In PHP, use foreach when you want to format each value yourself, print_r() for a readable representation with keys, or var_dump() when you need diagnostic details such as value types. The examples below use the same list of flowers so you can compare their output.

Use foreach Loop to Echo or Print an Array in PHP

A foreach loop assigns each value in the array to a loop variable, one iteration at a time. That variable is available inside the loop body, where you can send it to echo, add labels, or apply other formatting. The loop handles the array traversal; you do not need to advance an array pointer yourself.

Run this example:

<?php
$flowers = ["Rose", "Lili", "Jasmine", "Hibiscus", "Tulip", "Sun Flower", "Daffodil", "Daisy"];
echo "The array is:\n";
foreach ($flowers as $flower) {
    echo $flower . "\n";
}
?>
The array is:
Rose
Lili
Jasmine
Hibiscus
Tulip
Sun Flower
Daffodil
Daisy

This prints only the values, in iteration order. Use the key => value form of foreach if you also need each key; the current example uses a numerically indexed array, so its values are displayed without index labels.

Use print_r() Function to Echo or Print an Array in PHP

print_r() produces a human-readable representation that includes each array key and its associated value. It is useful when you want to inspect the overall contents without writing a loop or formatting each item. The second argument, often called the return parameter, is optional and defaults to false: the function prints the representation. When you pass true, it returns that representation as a string instead, which you can store or combine with other text.

<?php
$flowers = ["Rose", "Lili", "Jasmine", "Hibiscus", "Tulip", "Sun Flower", "Daffodil", "Daisy"];
print_r($flowers);
?>
Array
(
    [0] => Rose
    [1] => Lili
    [2] => Jasmine
    [3] => Hibiscus
    [4] => Tulip
    [5] => Sun Flower
    [6] => Daffodil
    [7] => Daisy
)

For example, $representation = print_r($flowers, true); captures the formatted text rather than printing it immediately. Choose this approach for a compact human-readable view; it does not label each value with its PHP type.

Use var_dump() Function to Echo or Print an Array in PHP

var_dump() is intended for debugging. It emits a diagnostic representation that identifies the array size, keys, value types, and string lengths. Unlike print_r(), it does not return that representation as a string for ordinary use; its return value is not the formatted diagnostic text. This makes it helpful for checking what values PHP holds, but less suitable for user-facing output.

<?php
$flowers = ["Rose", "Lili", "Jasmine", "Hibiscus", "Tulip", "Sun Flower", "Daffodil", "Daisy"];
var_dump($flowers);
?>
array(8) {
  [0]=>
  string(4) "Rose"
  [1]=>
  string(4) "Lili"
  [2]=>
  string(7) "Jasmine"
  [3]=>
  string(8) "Hibiscus"
  [4]=>
  string(5) "Tulip"
  [5]=>
  string(10) "Sun Flower"
  [6]=>
  string(8) "Daffodil"
  [7]=>
  string(5) "Daisy"
}

Use foreach when you need control over how values are displayed, print_r() for a readable snapshot of keys and values, and var_dump() to inspect types and lengths while debugging. These examples show flat string values; for nested arrays, each inspection function also represents nested contents, while custom formatting with foreach requires you to decide how to traverse them.