PHP and MySQL (55 Blogs) Become a Certified Professional

How to Implement print_r in PHP?

Published on Sep 16,2019 8.5K Views

In order to display array structure and values in PHP, we can either use print_r() or var_dump() statement so that we would be able to see or check the structure and values of an array in a human-readable format on the screen. Usually var_dump() statement gives more information than print_r(). In this article, we will discuss the following topics:


print_r in php

It is an inbuilt function that is used in PHP to print or display the information stored in a variable. Basically it prints human-readable information about a variable. If the variable is a string, integer or float, the value itself will be printed.

PHP- print_r in php - edureka


If an array is given, values will be presented in a format that shows keys and elements. Similar notation is used for objects.


print_r(variable, isStore)

variable: It is a mandatory parameter that specifies the variable to be printed.
isStore: It is an optional parameter which is of a boolean type whose default value is FALSE and is used to store the output of the print_r() function in a variable rather than printing it. print_r() function will return the output which it is supposed to print if this parameter is set to TRUE.

The below example demonstrates the usage of print_r in PHP. by printing string variable, integer variable and array:

$str = "This is a string"; //string
$integ = 30;//integer
$arra = array('0' => "ashok", '1' => "sushma", '2' => "charan");// array
echo "<br>";


Output - print_r in php - edureka


Let’s see the demonstration of an example by setting isStore to true so that it can store the output of the print_r() function in a variable rather than printing it.

$arr = array('0' => "ashok", '1' => "sushma", '2' => "charan");
$var = print_r($arr,true);
echo $var;



Difference between echo, print, print_r and var_dump in PHP

print and echo are almost same. Both of them are language constructs that display strings. Generally, print has a return value of 1 so that it can be used in expressions whereas echo has a void return type. Multiple parameters can be passed to echo. echo is slightly faster than print. Detailed dump of a variable can be printed using var_dump, including its type and the type of any sub-items if it’s an array or an object.

A variable in a more human-readable form using print_r: strings are not quoted, type information is omitted, array sizes aren’t given, etc. var_dump is usually more useful than print_r while debugging. It can be used when we don’t know exactly what values/types you have in your variables.

With print_r we can’t tell the difference between 0 and 0.0, or false and ‘ ‘. Below example demonstrates the difference between print_r and var_dump in PHP.

$val = array(0, 0.0, false, '');
print_r ($val);


Output 3 - print_r in php - edureka


With this we come to an end of this article. I hope you understood about print_r in php, difference between echo, print, var_dump and print_r in PHP.

If you found this PHP blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”Print_r in PHP” and I will get back to you.


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.