PHP | print_r() Function

The print_r() function is a built-in function in PHP and is used to print or display information stored in a variable.

Syntax:

print_r( $variable, $isStore )

Parameters: This function accepts two parameters as shown in above syntax and described below.



  1. $variable: This parameter specifies the variable to be printed and is a mandatory parameter.
  2. $isStore: This an option parameter. This parameter is of 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. If this parameter is set to TRUE then the print_r() function will return the output which it is supposed to print.

Return Value: If the $variable is an integer or a float or a string the function prints the value of the variable. If the variable is an array the function prints the array in a format which displays the keys as well as values, a similar notation is used for objects. If the parameter $isStore is set to TRUE then the print_r() function will return a string containing the information which it is supposed to print.

Below programs illustrate the print_r() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate
// the print_r() function
  
// string variable
$var1 = "Welcome to GeeksforGeeks";
  
// integer variable
$var2 = 101;
  
// array variable
$arr = array('0' => "Welcome", '1' => "to", '2' => "GeeksforGeeks");
  
// printing the variables
print_r($var1);
echo"\n";
print_r($var2);
echo"\n";
print_r($arr);
  
?>

chevron_right


Output:

Welcome to GeeksforGeeks
101
Array
(
    [0] => Welcome
    [1] => to
    [2] => GeeksforGeeks
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate the print_r()
// function when $isStore is set to true
  
// array variable
$arr = array('0' => "Welcome", '1' => "to",
                     '2' => "GeeksforGeeks");
                       
// storing output of print_r() function
// in another variable
$results = print_r($arr, true); 
  
echo $results;
  
?>

chevron_right


Output:

Array
(
    [0] => Welcome
    [1] => to
    [2] => GeeksforGeeks
)

Reference:
http://php.net/manual/en/function.print-r.php



My Personal Notes arrow_drop_up

Sudipta sahana BTech 2nd year Cse student

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.