English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP print_r() function usage and example

PHP available functions

print_r() The function is used to print variables in a more easily understandable form.

PHP version requirement: PHP 4, PHP 5, PHP 7

Syntax

bool print_r ( mixed $expression [, bool $return ] )

Parameter description:

  • $expression: The variable to be printed. If a string, integer, or float type variable is given, the value of the variable itself will be printed. If an array is given, it will be displayed in a certain format with keys and elements. Objects are similar to arrays.
  • $return: Optional, if set to true, the result is not output but assigned to a variable, false outputs the result directly.

Return value

$return if set to true Only returns a value, which is a string message that is easy to understand.

Online example

<?php
$a = array('a' => 'apple', 'b' => 'banana', 'c' => array('x', 'y', 'z'));
print_r($a);
?>

The output is:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)

Set the $return parameter:

<?php
$b = array('m' => 'monkey', 'foo' => 'bar', 'x' => array('x', 'y', 'z'));
$results = print_r($b, true); // $results contains the output of print_r
?>

The above information does not produce any output because the output is assigned to the $results variable.

PHP available functions