Content · Programming · PHP

PHP print_r() Function: Usage and Examples

Learn PHP print_r() through clear code examples, practical usage, important limitations and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

print_r() creates a readable representation of an array or object for debugging. This guide explains the function with focused examples and the decisions that matter in real applications.

What Is print_r() in PHP?

Creates a readable representation of an array or object for debugging.

A typical call looks like this:

<?php

print_r($value, [$return]);

The input arguments shown in the call identify the relevant connection, resource, value, path or configuration for the operation. Pay attention to argument order and to the return value demonstrated below.

Example 1: Basic Usage

This first example uses print_r() directly to demonstrate its basic purpose.

<?php

$profile = ["name" => "Sam", "active" => true]; print_r($profile);

Output:

Array
(
    [name] => Sam
    [active] => 1
)

The returned value illustrates creates a readable representation of an array or object for debugging. Never print secrets or unescaped user data into public HTML.

Example 2: A Different Input or Option

The next example changes the input or option, showing another supported use of print_r().

<?php

$values = [1, 2, 3]; $text = print_r($values, true); echo strlen($text);

Output:

49

The returned value illustrates creates a readable representation of an array or object for debugging. Never print secrets or unescaped user data into public HTML.

Practical Example: Applying the Function

In application code, print_r() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.

<?php

$settings = ["theme" => "dark"]; echo "Settings: " . print_r($settings, true);

Output:

Settings: Array
(
    [theme] => dark
)

The returned value illustrates creates a readable representation of an array or object for debugging. Never print secrets or unescaped user data into public HTML.

Important Behavior and Edge Cases

  • Main behavior: Creates a readable representation of an array or object for debugging.
  • Important limitation: Never print secrets or unescaped user data into public HTML.
  • Requirements: This operation is available in standard PHP; validate inputs and handle failures where relevant.

Comparison With Related Functions

Function or ApproachWhen to Use It
print_r()Creates a readable representation of an array or object for debugging.
var_exportFormats a PHP value as a representation resembling valid PHP code, unlike print_r() output.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the specific limits or preconditions of print_r().Never print secrets or unescaped user data into public HTML.
Assuming every environment produces the same result.This operation is available in standard PHP; validate inputs and handle failures where relevant.

Key Takeaways

  • Creates a readable representation of an array or object for debugging.
  • Never print secrets or unescaped user data into public HTML.
  • This operation is available in standard PHP; validate inputs and handle failures where relevant.

Use print_r() when its behavior matches your task, and verify the relevant return value before taking further action. The output depends on the local configuration when it uses resources, files or an interactive terminal.

Share this resourceSend this page to someone who may find it useful.