PHP print_r() Function
PHP print_r() Function
The print_r() function in PHP displays the information stored in a variable.
Syntax
print_r (mixed $expression [, bool $return] )
Parameter
expression(required)- This parameter represents the expression to be printed.
return(optional)- This parameter is used to store the output of the print_r() function in a variable rather than printing. It is of boolean type whose default value is FALSE. If this parameter is set to TRUE then the print_r() function will return the output which it is supposed to print.
Return
This function returns the value itself if the specified expression if string, integer or float. If the expression is an array, this function will return the array in a format which displays the keys as well as values.
This function returns the output which it is supposed to print if the “return” parameter is set to TRUE.
Example 1
<?php // initializing the expression with string variable $exp = "Welcome to TutorialAndExample"; // printing the expression print_r($exp); //returns the value itself ?>
Output
Welcome to TutorialAndExample
Example 2
<?php
// initializing the expression with array
$arr = array('0' => "Hello", '1' => "TutorialAndExample!", '2' => "Good",'3' => "Job.");
// printing the expression
print_r($arr); //returns the array in a format which displays the keys as well as values.
?>
Output
Array ( [0] => Hello [1] => TutorialAndExample! [2] => Good [3] => Job. )
Example 3
<pre>
<?php
//initializing the expression
$fruits = array ('Summer Fruits' => array ('Mango', 'Watermelon', 'Lichi'), 'Winter Fruits' => array ('Orange', 'Apple', 'Guava'));//array inside array
print_r ($fruits);
?>
</pre>
Output
<pre> Array ( [Summer Fruits] => Array ( [0] => Mango [1] => Watermelon [2] => Lichi ) [Winter Fruits] => Array ( [0] => Orange [1] => Apple [2] => Guava ) ) </pre>
Example 4
<pre>
<?php
//initializing the expression
$fruits = array ('Summer Fruits' => array ('Mango', 'Watermelon', 'Lichi'), 'Winter Fruits' => array ('Orange', 'Apple', 'Guava'));//array inside array
//passing boolean value true
print_r ($fruits,true);// will return the information
?>
</pre>
Output
<pre> </pre>
