PHP and MySQL (54 Blogs) Become a Certified Professional

How to Implement print_r in PHP?

Published on Sep 16,2019 680 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

Syntax:

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:

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

Output:

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.

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

Output:

 

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.

<?php
$val = array(0, 0.0, false, '');
var_dump($val);
echo"<br>";
print_r ($val);
?>

Output:

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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
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.