The php print_r() function allows us to get human-readable information about our php variables. We can print arrays and objects with print_r().

$num = 5;
$string = "This is a string";
$array = ["This","is","an","array"];

print_r($num);
print_r($string);
print_r($array);

//Output:
5
This is a string
Array
(
    [0] => This
    [1] => is
    [2] => an
    [3] => array
)

When working with variables in programming, the ability to be able to print them out in a human-readable way is very useful.

The php print_r() function allows us to print information about a variable in a human-readable way.

With print_r(), we can output all types of variables, including arrays and objects.

If a string, int, or float variable is passed to print_r(), the value will be printed. If an array is passed, the elements will be printed in a format that shows the keys and values.

Below is an example of using print_r() to output different types of variables.

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}


$num = 5;
$string = "This is a string";
$array = ["This","is","an","array"];

print_r(new exampleClass);
print_r($num);
print_r($string);
print_r($array);

//Output:
exampleClass Object
(
    [id] => 
    [name:exampleClass:private] => 
)
5
This is a string
Array
(
    [0] => This
    [1] => is
    [2] => an
    [3] => array
)

Returning the Information from print_r() Instead of Printing it in php

By default, print_r() prints human-readable information.

We can capture that information in a new variable by passing a second parameter to print_r().

Below is an example in php of how to return the information produced by print_r() in a new variable.

$array = ["This","is","an","array"];

$new_var = print_r($array, true);

echo $new_var;

//Output:
Array
(
    [0] => This
    [1] => is
    [2] => an
    [3] => array
)

Printing Arrays with print_r() in php

We can print an array in php is with the print_r() function.

As we know, print_r() prints human-readable information about a variable.

So, if you have an array, you can pass it to print_r() and get a human-readable array which shows all of the array keys and array values.

Below is an example in php showing how to print an array with print_r().

$array = ["This","is","an","array"];

print_r($array);

//Output:
Array
(
    [0] => This
    [1] => is
    [2] => an
    [3] => array
)

Printing Objects with print_r() in php

print_r() lets us print objects in a human-readable way.

If you have an object, you can pass it to print_r() and get a listing the public and private variables in the object.

Below is an example in php showing how to print an object with print_r().

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

print_r(new exampleClass);

//Output:
exampleClass Object
(
    [id] => 
    [name:exampleClass:private] => 
)

Hopefully this article has been useful for you to learn about the print_r() function in php and how to print variables in a readable way in your php programs.

Categorized in:

PHP,

Last Update: March 13, 2024