Introduction to print_r() in PHP
An in-built function of PHP i.e. print_r(), is used to generate human-readable information about any given variable in a script. The output from the function can be stored in a variable or can be printed on the output window. The functionality of either to store the value or to display the values is determined by a number of input arguments given to the function. This function is supported in PHP version 4 onwards i.e. PHP4, PHP5, and PHP7.
Syntax:
The function print_r() can be implemented in any PHP script with the syntax as shown below:
print_r ( mixed $variable_name , bool $returnType) : mixed
where variable_name value is a mandatory parameter to be provided whereas $returnType is an optional one.
Parameters of print_r() in PHP
There are two parameters that are acceptable by the method as input arguments.
1. $variable_name
This parameter is the mandatory input argument for print_r() method. It specifies the variable about which the function needs to extract the information in a human-readable format. In the case of a class variable, this method also captures the properties of the class members.
2. $returnType
This parameter is an optional input argument for this function. This function is used to decide to store the output or to print it.
This is a Boolean type parameter. The default value of this parameter is set to FALSE.
4.5 (5,726 ratings)
View Course
Value of ReturnType | Description |
TRUE | The function returns a value that can be stored in a variable. |
FALSE | The function prints the output and the value cannot be captured or stored. |
Return Value:
The return value of the function depends on the type of the variable and value of returnType as input argument. If given variable is of type string, integer or float; the return value is the variable itself as it is. In case of array or object type of variable input, the return values from the function are the keys and the elements when the returnType is set to FALSE.
When the returnType is set to TRUE, print_r() results in a storable outcome.
Examples to Implement of print_r() in PHP
Below are the examples of print_r() in PHP:
Example #1
The below code snippet is designed to illustrate the functionality of print_r() in order to display information about a string variable, an integer variable, and an array input. This can be achieved by including only the input variable as an input parameter to the function call. In this case, the default value of the returnType parameter is FALSE.
Code:
<?php
// PHP program to illustrate the print_r() function to exhibit printing functionality :
// Declaring a string variable
$input_str = "An information string";
// Declaring a integer variable
$input_int= 101;
// Declaring an array variable
$input_arr = array("Count1"=>"35", "Count2"=>"45", "Count3"=>"55");
// printing the variables
print_r("Printing the string variable information");
echo"\n";
print_r($input_str);
echo"\n";
echo"\n";
print_r("Printing the integer variable information");
echo"\n";
print_r($input_int);
echo"\n";
echo"\n";
print_r("Printing the array variable information");
echo"\n";
print_r($input_arr);
?>
Output:
As discussed earlier, in the case of string and integer variable, the information is printed as it is, whereas the output for the array variable is printed in the form of key and element value along with datatype associated with it.
Example #2
By default the value is FALSE. In order to achieve the functionality to capture and store the output from print_r() in a variable, this returnType parameter needs to be set as TRUE in the function call.
Code:
<?php
// PHP program to illustrate the print_r() function exhibiting the storing functionality
// Declaring a string variable
$input_str = "An information string";
// Declaring an integer variable
$input_int= 5040;
// Declaring an array variable
$input_arr = array("element1"=>"31", "element2"=>"41", "element3"=>"51");
// Capturing and storing the output in different variables
print_r("Capturing the integer variable information");
echo"\n";
//Storing the integer variable output
$input_int_cap=print_r($input_int,true);
print_r($input_int_cap);
echo"\n";
echo"\n";
print_r("Capturing the string variable information");
echo"\n";
//Storing the string variable output
$input_str_cap=print_r($input_str,true);
print_r($input_str_cap);
echo"\n";
echo"\n";
print_r("Capturing the array variable information");
echo"\n";
//Storing the array variable output
$input_arr_cap=print_r($input_arr,true);
print_r($input_arr_cap);
?>
Output:
The integer and string variables information in storing variables of integer and string datatypes respectively are captured as it is; whereas the output from the array variable is stored in array type storing variable, in the form of key and element along with the data type associated with it.
The display result is the output of print_r() used with the storing variables.
Additional Note:
- This method can also show properties of private and protected properties of an object whereas it has restricted features that it will not show results for static class members.
- In PHP5, print_r() can be used for static class member variables by using Reflection class.
- Print_r() is used in debugging any php script.
- When the returnType parameter is used in the function call, print_r() uses internal output buffering. Hence it cannot be used within an ob_start() callback method.
Recommended Article
This is a guide to the print_r() in PHP. Here we discuss the Parameters of print_r() in PHP and its examples along with Code Implementation. You can also go through our other suggested articles to learn more-