• PHP Video Tutorials

PHP - print_r() Function



Definition and Usage

The function print_r(), prints information about a variable that is human-readable. This function also shows protected and private properties of objects. Static class members will not be shown.

Syntax

string|bool print_r ( mixed $value , bool $return = false )

Parameters

Sr.No Parameter Description
1

value

Mandatory. The expression to be printed.

2

return

Optional. When set to true, this function will return the information (not print it). This information can be collected or stored. Default is false

Return Values

This function returns type depends on the type of value passed:

  • If value is a string, int or float, the value itself will be printed
  • If value is an array or objects, values will be presented in a format that shows keys and elements
  • If the return parameter is true, this function will return a string. Otherwise, the return value is true.

Dependencies

PHP 4 and above

Example

Following example demonstrates print_r() use:

  <?php
  $a = array ('t' => 'tutorials', 'p' => 'point', 'c' => array ('a', 'b', 'c'));
  print_r ($a);
  ?>

Output

This will produce following result −

Array ( [t] => tutorials [p] => point [c] => Array ( [0] => a [1] => b [2] => c ) )

Example

Following example demonstrates print_r() use with return parameter set to true:

  <?php
  $b = array('1' => 'Pune', '2' => 'Bengaluru', 'x' => array ('x', 'y', 'z'));
  $results = print_r ($b, true);// $results contains output from print_r
  var_dump($results);
  ?>

Output

This will produce following result −

string(148) "Array ( [1] => Pune [2] => Bengaluru [x] => Array ( [0] => x [1] => y [2] => z ) ) "
Advertisements