print_r()

Description

This function displays the value of the passed expression in a “human-readable” form. If passed expressions of types boolean, integer, float, and string, the value itself will be printed. If passed an expression of type array the array’s indexes and values will be shown. Object expressions print (???). Resource expressions print the resource ID (???)

Syntax


mixed print_r( mixed $expr [, bool $return = false ] )

Parameters

$expr
The expression to print
$return
This parameter can be passed one of the follow values:

  • false (default) the $expr will be printed
  • true the output will be returned to the caller as a string

Return Value

mixed
One of the following values will be returned based on the value of the $return parameter.

  • string if $return is passed as true, the result will be returned as a string
  • bool if $return is passed as false, the result will be true

Examples

1. Call print_r() with Various Variable Types

This example passes variables of various types to print_r() to see the effect. The $return argument is omitted, causing the function to print its output rather than return it to the caller.

<?php
$boolVal     = true;
$intVal      = 6;
$floatVal    = 3.14;
$stringVal   = "Hello";
$arrayVal    = array( 1, "two", 3.4 );
$objectVal   = new stdClass;
$resourceVal = fopen("./data.txt", "r");
$nullVal     = null;

print_r( $boolVal );     echo "\n";
print_r( $intVal );      echo "\n";
print_r( $floatVal );    echo "\n";
print_r( $stringVal );   echo "\n";
print_r( $arrayVal );    echo "\n";
print_r( $objectVal );   echo "\n";
print_r( $resourceVal ); echo "\n";
print_r( $nullVal );     echo "\n";

fclose( $resourceVal );
?>

As can be seen from the output below, print_r() prints the values of the passed arguments. Scalar arguments print in an unformatted style, whereas compound arguments use indentation to show the structure of the expression.

1
6
3.14
Hello
Array
(
    [0] => 1
    [1] => two
    [2] => 3.4
)

stdClass Object
(
)

Resource id #5

2. Example heading

Description of example….



See Also

References