Menu Home

print_r

osbo.com | PHP | Functions | print_r

Prints human-readable information about a variable

Syntax

print_r ( mixed $expression [, bool $return = FALSE ] ) : mixed

Parameters

expression

The expression to be printed.

return

If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.

Return

If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects. When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.

Examples

expression | bool

<?

$expression = false;

print_r($expression);

?>

			
			
		

expression | int

<?

$expression = 0;

print_r($expression);

?>
0

expression | float

<?

$expression = 0.0;

print_r($expression);

?>
0

expression | string

<?

$expression = "0";

print_r($expression);

?>
0

expression | array

<?

$expression = array(0, 1, array(0, 1));

print_r($expression);

?>
Array
(
    [0] => 0
    [1] => 1
    [2] => Array
        (
            [0] => 0
            [1] => 1
        )

)

expression | object | standard class

<?

$expression = new stdclass;
$expression->var = 0;

print_r($expression);

?>
stdClass Object
(
    [var] => 0
)

expression | object | custom class

<?

class myclass {
    public $var;
}

$expression = new myclass;
$expression->var = 0;

print_r($expression);

?>
myclass Object
(
    [var] => 0
)

return | false

<?

class myclass {
    public $var;
}

$expression = new myclass;
$expression->var = 0;
$return = false;

$output = print_r($expression, $return);

echo "output: " . PHP_EOL . $output;

?>
myclass Object
(
    [var] => 0
)
output: 
1

return | true

<?

class myclass {
    public $var;
}

$expression = new myclass;
$expression->var = 0;
$return = true;

$output = print_r($expression, $return);

echo "output: " . PHP_EOL . $output;

?>
output: 
myclass Object
(
    [var] => 0
)

Internal

var_dump

var_export

External

PHP