PHP print_r() Function

Print human-readable information about a variable

Try print_r() - edit and run
Output
Array
(
    [name] => Alice
    [roles] => Array
        (
            [0] => admin
            [1] => editor
        )

    [active] => 1
)

What does print_r() do?

print_r() prints a variable in a human-readable format. It is the quickest way to inspect an array or object while debugging. It shows keys and values but not types: true prints as "1" and false/null print as nothing.

Syntax

print_r(mixed $value, bool $return = false): string|true

Parameters

  • $value - The variable to print: array, object, string, number, etc.
  • $return - When true, the output is returned as a string instead of printed.

Return value

With $return = true, the formatted string; otherwise it prints and returns true.

print_r() examples

Every example below shows the exact output it produces. Paste any of them into the editor above (or the online compiler) to run it.

1. Print a nested array

<?php
$user = ["name" => "Alice", "roles" => ["admin", "editor"], "active" => true];
print_r($user);

Output:

Array
(
    [name] => Alice
    [roles] => Array
        (
            [0] => admin
            [1] => editor
        )

    [active] => 1
)

2. Return the output as a string

<?php
$data = ["a" => 1, "b" => null, "c" => false];
$text = print_r($data, true);
echo "Captured " . strlen($text) . " characters\n";
echo $text;
// In a web page, wrap it in <pre> to keep the indentation:
// echo "<pre>" . print_r($data, true) . "</pre>";

Output:

Captured 47 characters
Array
(
    [a] => 1
    [b] =>
    [c] =>
)

3. print_r() vs var_dump() vs var_export()

<?php
$value = [1, "1", 1.5, true, null];
print_r($value);
var_dump($value);
var_export($value);
echo "\n";

Output:

Array
(
    [0] => 1
    [1] => 1
    [2] => 1.5
    [3] => 1
    [4] =>
)
array(5) {
  [0]=>
  int(1)
  [1]=>
  string(1) "1"
  [2]=>
  float(1.5)
  [3]=>
  bool(true)
  [4]=>
  NULL
}
array (
  0 => 1,
  1 => '1',
  2 => 1.5,
  3 => true,
  4 => NULL,
)

4. Objects show visibility

<?php
class Point {
    public $x = 1;
    protected $y = 2;
    private $z = 3;
}
print_r(new Point());

Output:

Point Object
(
    [x] => 1
    [y:protected] => 2
    [z:Point:private] => 3
)

Common mistakes and gotchas

  • Types are hidden: "1", 1 and true look identical. Use var_dump() when types matter.
  • Browsers collapse whitespace: wrap the output in <pre> when printing to an HTML page.
  • Production: never print internal data to users; log it with error_log(print_r($data, true)) instead.

PHP print_r() FAQ

What is the difference between print_r() and var_dump()?

print_r() shows keys and values only. var_dump() also shows types and lengths, which is better for debugging comparisons.

How do I print an array in PHP?

Use print_r($array) for a quick look, var_dump($array) for types, implode(", ", $array) for a flat list, or json_encode($array, JSON_PRETTY_PRINT) for JSON.

How do I save print_r() output to a variable?

Pass true as the second argument: $s = print_r($data, true);