• 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
  • VBA
  • 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.  Get Current Location Latitude Longitude in php
  • 2.  Capitalize First Letter of String with php ucfirst() Function
  • 3.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 4.  php array_filter() – Filter Array Elements with Function in php
  • 5.  Replace Underscore with Space in php String
  • 6.  Remove Last Character from String Using php
  • 7.  php array_pop() – Remove Last Element from Array
  • 8.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 9.  php array_walk() – Modify Elements in Array with Callback Function
  • 10.  php switch case – How to Use Switch…Case Statements 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy