PHP print_r() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, print_r() prints a human-readable version of a value. It is mostly used to look inside arrays and objects while you are writing and debugging code.

For simple values such as strings and numbers it prints them as they are. For arrays and objects it prints an indented list of keys and values. If you pass true as the second argument, it returns the text as a string instead of printing it.

Syntax

Call print_r() with the values you need. Start with this line.

PHP
print_r($data);
CallWhat it does
print_r($array)Prints the array as an indented list, then returns true.
print_r($array, true)Does not print anything. It returns the text as a string.
print_r('Hello')Prints Hello exactly as it is.

print_r() hides the types. true prints as 1, while false and null print as nothing. Use var_dump() when you need to see the exact types, and wrap the output in <pre> tags in a browser to keep the layout.

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

Parameters

ParameterDescription
valueRequired. The value to print. It can be of any type.
returnOptional. When true, print_r() returns the text instead of printing it. The default is false.

Return Value

Return ValueDescription
stringThe text version of the value, only when return is set to true.
trueReturned after printing, when return is false (the default).

PHP Examples

Example 1 — Printing an Array

Print a simple array, then a plain string.

PHP
<?php
$fruits = ['apple', 'banana'];

print_r($fruits);
print_r('Hello');
echo PHP_EOL;

How It Works

  1. The array has two items. PHP numbers the keys 0 and 1 automatically.
  2. print_r() prints Array and then each key and value. The format is key => value inside parentheses.
  3. A string is printed as it is. print_r('Hello') shows only Hello with no quotes.
  4. echo PHP_EOL adds the final new line. print_r() does not add one after plain strings.

Example 2 — Capturing the Output and Nested Arrays

Pass true as the second argument to store the text in a variable. The data also contains a boolean and a nested array.

PHP
<?php
$data = ['a' => 1, 'ok' => true, 'list' => [10, 20]];

$text = print_r($data, true);

echo 'Captured:', PHP_EOL;
echo $text;
echo '[', print_r(false, true), ']', PHP_EOL;

How It Works

  1. The second argument is true. Nothing is printed by print_r(), and the text is stored in $text.
  2. echo prints the stored text later. Nested arrays are indented one level deeper.
  3. The boolean true shows as 1. print_r() does not print the word true.
  4. print_r(false, true) returns an empty string. That is why the last line shows empty brackets.

Example 3 — Printing an Object

Objects show their class name and every property. Private and protected properties are labeled.

PHP
<?php
class Point {
    public $x = 1;
    protected $y = 2;
    private $z = 3;
}

print_r(new Point());

How It Works

  1. The class has three properties. They are public, protected and private.
  2. print_r() starts with the class name. The words Point Object tell you what you are looking at.
  3. Each property is listed. Protected and private ones are labeled so you can tell them apart.
  4. The output ends with a closing parenthesis and a new line. It is easy to read while debugging.

Frequently Asked Questions

It prints a human-readable version of a value. For arrays and objects it shows an indented list of keys and values, which makes it a favorite for debugging.
Pass true as the second argument, like print_r($data, true). The function returns the text instead of printing it, so you can store it, log it or echo it later.
print_r() gives a short, readable layout and hides types. var_dump() shows the type and size of every value, such as int(5) or string(3) 'abc', so it is better when you need exact details.
print_r() converts the value to a string, and false becomes an empty string. true becomes 1. Use var_dump() or var_export() to see the real boolean.
Browsers collapse whitespace, so wrap the output in <pre> tags: echo '<pre>', print_r($data, true), '</pre>'. Use htmlspecialchars() if the data might contain HTML.

Next: serialize()

serialize() converts a PHP value into a string that can be stored or sent somewhere. Later.

serialize() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful