The Ultimate Guide To print_r in PHP

In this article, you going to see a complete discussion regarding print_r in PHP in-depth and in detail step by step using a simple example.

If you are looking for this topic then you are at a right place.

Let’s start with topic of print_r in PHP

It is a function use to print the information about any variable.

It give the information in such a way that every user get completely understand easily about any variable.

Below is the syntax from which you can understand it’s declaration.

print_r(variable, return);
ParameterDescription
variableIt is required and it represents the variable which gives output
returnIt is optional and it is default set as false and if it is set to true then it will return information but not print it.
Parameter Value print_r in php

Below is the simple example from which you can understand.

<?php
$a = array("one", "two", "three");
print_r($a);
echo "<br>";
$b = array("Jack"=>"35", "James"=>"37", "Jig"=>"43");
print_r($b);
?>

Output :

Array ([0] => one [1] => two [2] => three)
Array ([Jack] => 35 [James] => 37 [Jig] => 43)

What is the difference between echo, print, and print_r in PHP?

echo: It is not a function. It is a language construct that displays strings. The return type is void. It cannot be used as a variable function in PHP. Display the output for the Parameter which is passed.

<?php 
$arr = array( "Jack", "James", "Jig");
echo "Array...\n";
   foreach( $arr as $value ) {
      echo "Value = $value \n";
   }
?>
Output :
Array...
Value = Jack
Value = James
Value = Jig

print: It is not a function. It is a language construct that displays strings. Always return value 1. It cannot be used as a variable function in PHP. Display the output for the Parameter which is passed.

<?php 
$arr = array( "Jack", "James", "Jig"); 
echo "\nDisplaying Array Values using print...\n";
   foreach( $arr as $value ) {
      print( "Value = $value \n");
   }
?> 
Output : 
Displaying Array Values using print...
Value = Jack
Value = James
Value = Jig

print_r: It is a function. It gives the output as detailed information regarding variables. you can also define as it gives the output as human-readable information about a variable.

<?php 
$arr = array( "Jack", "James", "Jig"); 
 echo "\nDisplaying Array Values using print_r...\n";
   print_r($arr);
?> 
Output : 
Displaying Array Values using print_r...
Array (
   [0] => Jack
   [1] => James
   [2] => Jig
)

How to Print an Array in PHP Using Print_r()?

It is very simple to print an array using print_r function.

To do this declare the array variable and assign value to that variable.

From the below example you can understand easily.

<?php 
$arr = array( "Jack", "James", "Jig"); 
 echo "\nDisplaying Array Values using print_r...\n";
   print_r($arr);
?>
Output : 
Displaying Array Values using print_r...
Array (
   [0] => Jack
   [1] => James
   [2] => Jig
)

What is the difference between Var_dump and Print_r?

var_dump: It is a function which displays structure data or you can say information about variable or expressions which include its type and value.

Arrays are explored recursively with values indented to show structure.

It also display the output regarding which array values and object properties are references.

print_r : It is a function Displays information about a variable that is simply get understand by humans. Array values presented in a format that shows keys and elements.

One more important point regarding this is that print_r(null) will return nothing whereas var_dump(null) returns NULL which is useful when debugging.

Below is the example from which you can understand easily

$obj = (object) array('one', 'two', 'three');
var_dump($obj); //will display below output in the screen.

object(stdClass)#1 (3) {
 [0]=> string(3) "one"
 [1]=> string(3) "two"
 [2]=> string(5) "three"
}
print_r($obj); //will display below output in the screen.
stdClass Object ( 
 [0] => one
 [1] => two
 [2] => three
)

How can we display information of a variable and readable by human with PHP?

The print_r function can be used to display the information of a variable and readable by a human.
It gives the output as detailed information regarding variables.

From below example you can understand easily.

<?php 
$arr = array( "One", "Two", "Three"); 
 echo "\nDisplaying Array Values using print_r...\n";
   print_r($arr);
?> 
Output : 
Displaying Array Values using print_r...
Array (
   [0] => One
   [1] => Two
   [2] => Three
)

What is the use of print_r in PHP?

The print_r in PHP used to display the information of a variable and readable by human.

What is Echo Pre in PHP?

The "<PRE>" tag doesn’t exist, but it’s probably the "<PRE>" HTML tag to put around debug output, to improve readability.

Below is example :

<?php
$arr = array( "One", "Two", "Three");  
echo '<pre>',print_r($arr,1),'</pre>';
?>
output :
Array
(
    [0] => One
    [1] => Two
    [2] => Three
)

How do I echo an array in PHP?

It is one of the important point in PHP is that one cannot directly echo an array.

If you directly echo an array it give an error as below.

Notice: Array to string conversion

So to echo an array we have to used one PHP function called print_r function.

From the above example you can understand regarding how to echo an array in PHP.

What is the use of var_dump in PHP?

It is used to display structure data or you can say information about variable or expressions which include its type and value.

Below is the example from which you can understand easily.

$obj = (object) array('red', 'blue', 'green');
var_dump($obj); //will display below output in the screen.

object(stdClass)#1 (3) {
 [0]=> string(3) "red"
 [1]=> string(4) "blue"
 [2]=> string(5) "green"
}

How to display data in Div using PHP

Conclusion :

Finally, we done with the discussion regarding all the point of print_r in PHP.

I hope you like this article and if you feel like we missed out on anything, please comment below.

Leave a Comment