Full Stack Web Development Internship Program
- 3k Enrolled Learners
- Live Class
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:
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.
If an array is given, values will be presented in a format that shows keys and elements. Similar notation is used for objects.
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); ?>
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; ?>
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); ?>
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.