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> 

Related Topics

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP intval() Function

The intval() Function in PHP is used to get the integer value for the given variable. This function should not be used on objects. Else it will emit an E_NOTICE level error and return 1. Syntax intval...

1 minute read.

PHP convert_uudecode() Function

PHP convert_uudecode() Function The convert_uudecode() function in PHP decodes a uuencoded string. Syntax convert_uudecode ( string $data ) Parameter data- This parameter represents the uuencoded data. Return This function returns the decoded data as a string or FALSE on failure. Example 1 <?php // initializing the string $str...

1 minute read.

PHP print() Function

PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...

1 minute read.

PHP compact() function

PHP compact() function The compact() function in PHP is used to create an array containing variables and their values. It returns the output array whose keys are the variable names, and their corresponding values are...

1 minute read.

PHP Functions

PHP functions are used to reuse piece of code many times.  There are various built-in functions in PHP. A function will be executed by a call to the function. Following are...

3 minutes read.

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function The array_diff_uassoc () Function in PHP is used to compare the keys and values of two (or more) arrays and returns the differences (an array containing the entries from the first array that...

1 minute read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

PHP array_udiff() Function

PHP array_udiff() Function The array_udiff() function in PHP calculates the difference of arrays by using a callback function for data comparison. Syntax array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func ) Parameter array1(required)- This array represents the first input array. array2(required)- This array represents the second input array. array3….(optional)- It...

1 minute read.

PHP floatval() Function

PHP floatval() Function The floatval() function in PHP is used to give the float value of a variable. Syntax floatval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that is to be converted. Return This function...

1 minute read.

PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters. The some predefined HTML entities that will be decoded are as follows: &amp; OR & (ampersand)&quot; OR " (double quote)&#039;...

2 minutes read.

PHP arsort() function

PHP arsort() function The arsort() function in PHP sorts an array in the reverse order and maintains the index association Syntax arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the behavior...

2 minutes read.

Python main() function

What is a main() function? In most of the programming languages, there is this one special function i.e., main() which is executed firstly everytime when we run the program. The special...

6 minutes read.

PHP Super Global Variables

There are various predefine “superglobals” in PHP. These variables can easily accessible in function or class. Following are the superglobals variables: $GLOBALS $_SERVER $_REQUEST $_POST $_GETetc. PHP $GLOBALS The “$GLOBAL” isa keyword that...

2 minutes read.

PHP natcasesort() Function

PHP natcasesort() Function The natcasesort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natcasesort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean...

1 minute read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 minutes read.

PHP convert_cyr_string() Function

PHP convert_cyr_string() Function The convert_cyr_string() function in PHP converts from one Cyrillic character-set to another. The supported characters set are given below: k - koi8-rw - windows-1251i - iso8859-5a - x-cp866d - x-cp866m...

1 minute read.

PHP strrchr() Function

PHP strrchr() Function The strrchr() function in PHP finds the last occurrence of a character in the specified string and returns the portion of haystack, which starts at the last occurrence of the needle and goes until the end of haystack....

1 minute read.

PHP key_exists() Function

PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array )  Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.