• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php print_r() Function- Get Information about Variables in php

php print_r() Function- Get Information about Variables in php

March 31, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  php array_map() – Create New Array from Callback Function
  • 2.  Remove Duplicates from Array in php with array_unique()
  • 3.  php json_encode() Function – Get JSON Representation of Variables in php
  • 4.  How to Get Current System IP Address in php
  • 5.  php Square Root with sqrt Function
  • 6.  Check if String Ends with Given String in php with str_ends_with()
  • 7.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 8.  Delete Cookie Using php
  • 9.  php floor – Round Down and Find the Floor of a Number in php
  • 10.  Add Months to Date in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy