Learn how to use PHP print_r function

Introduction

In this PHP Tutorial, I'm Publishing some information about "PHP print_r() function", which includes Syntax, Examples, Usage, Related functions and Some Details associated with it. "print_r" Function is a Member of Variable Handling Functions in PHP. This type/category of Functions basically deals with data stored in and associated with variables, whether, Variable is an array, String or an Object.

Definition of print_r Function

The PHP print_r function is used to display data stored in an array on screen i.e. Structured Visual representation of array data in an Human Readable form which is shown below.

Sample Function Output - Visual Representation

Array
(
[a] => My
[b] => Name
[c] => Array
     (
     [0] => Is
     [1] => itech
     )
) 

Here, Below is the syntax of PHP print_r function in which $expression will accept array as INPUT type and $return will accept either "true" or "false" as INPUT.

Reference Notes :

  • If you'll give string, integer or float as input type instead of array in $expression, then, VALUE itself will be printed.
  • If $return is set to "true" then "print_r" function will return an output instead of printing it directly on the screen. You can do so, if you want to save returned output in form of variable value.

Syntax of print_r Function

mixed print_r ( mixed $expression [, bool $return = false ] );

Usage and Examples

Here, Below is an basic example showing the usage of "print_r" function which includes both PHP Code (PHP Script) and Screen Output. We are creating two type of arrays

  1. Simple Array,
  2. And Multidimensional Array,

Then, We are printing both of them on screen using print_r function directly. After that, We are creating two more variable $arrayS and $arrayMD, which shows the Usage of storing output in the form of variables and then printing it via PHP echo statement.

PHP Script

<?php
$array1=array(	
			"a" => "Feels Good", 
			"b" => "to be a", 
			"c" => "Programmer"
		);
$array2=array(	
			"a" => "I AM" ,
			"b" => array(
					"c" => "NOT SO GOOD" ,
					"d" => array(
							"e" => "IN PROGRAMMING"
						)
				)
		);
print_r($array1);
print_r($array2);

//Below we are storing the print_r output in the form of variables.
$arrayS=print_r($array1, true);
$arrayMD=print_r($array2, true);

//Now, We are outputting the variables
echo $arrayS;
echo $arrayMD;
?>

Output of Above PHP Code

Array
(
    [a] => Feels Good
    [b] => to be a
    [c] => Programmer
)
Array
(
    [a] => I AM
    [b] => Array
        (
            [c] => NOT SO GOOD
            [d] => Array
                (
                    [e] => IN PROGRAMMING
                )

        )

)

-----------------------------------------------------------

Array
(
    [a] => Feels Good
    [b] => to be a
    [c] => Programmer
)
Array
(
    [a] => I AM
    [b] => Array
        (
            [c] => NOT SO GOOD
            [d] => Array
                (
                    [e] => IN PROGRAMMING
                )

        )

)

Related PHP Functions

  1. ob_start() - Turn on output buffering,
  2. var_dump() - Dumps information about a variable,
  3. var_export() - Outputs or returns a parsable string representation of a variable,
  4. Echo () PHP Function - Output one or more strings,
  5. Print () PHP Function - Output a string,
  6. Array () PHP Function - Creates an ARRAY.

More by this Author


Comments 2 comments

Tam Finland 4 years ago

Hi please you have to pay attention that the code you write is not the same as the output you showed

the missing code is " pre " ," /pre "


itech profile image

itech 4 years ago from New Delhi, India Author

Just view the source code of html page on which you printed your output.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working