Home >Backend Development >PHP Tutorial >Print_r() in PHP
PHP developers use the print_r() function to generate human-readable information about a variable in a script. They can store the function’s output in a variable or print it directly on the output window. The behavior of storing or displaying the values depends on the input arguments provided to the function. It’s important to note that print_r() has been supported since PHP version 4, including PHP4, PHP5, and PHP7.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
You can implement the print_r() function in any PHP script using the following syntax:
print_r ( mixed $variable_name , bool $returnType) : mixed
The value of the variable_name parameter is mandatory, whereas $returnType is optional.
Two parameters are acceptable by the method as input arguments.
This parameter is the mandatory input argument for the 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.
This function has an optional input argument that allows you to decide whether to store or print the output. You can use this parameter to determine the desired behavior.
This is a Boolean-type parameter. The default value of this parameter is set to FALSE.
|
Description | ||||||
TRUE | The function provides a return value that you can store in a variable. | ||||||
FALSE | The function prints the output; you cannot capture or store the value. |
Return Value:
The return value of the function depends on the type of the variable and the value of the returnType as the input argument. If a given variable is of type string, integer, or float, the return value is the variable itself as it is. When you set the return type to FALSE, and the input variable is an array or object, the function will return the keys and elements as the output values.
When the returnType is set to TRUE, print_r() results in a storable outcome.
Note: By default, the value for returnType is FALSE. Thus the default functionality of the print_r() method is the print/display the information of the given variable. To configure the function to capture the output and store it in different variables, the developer needs to use the parameter returnType in its print_r() function call by setting the value to TRUE.Below are the examples of print_r() in PHP:
The code snippet below illustrates the functionality of print_r() in displaying information about a string variable, an integer variable, and an array input. To achieve this, you must include only the input variable as a parameter in 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 mentioned earlier, when handling string and integer variables, the information is printed as it is. However, when it comes to an array variable, the output displays the data in the format of key-value pairs and their corresponding data types.
By default, the value is FALSE. To capture and store the output from print_r() in a variable, you need to set the returnType parameter 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:
Variables of their respective data types capture and store the information from integer and string variables. On the other hand, when dealing with an array variable, the function stores the output in a dedicated variable specifically designed to hold arrays. This storage encompasses the key-value pairs and the associated data types of each element.
The display result refers to the output produced using print_r() with storing variables.
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-
The above is the detailed content of Print_r() in PHP. For more information, please follow other related articles on the PHP Chinese website!