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 uksort() Function

PHP uksort() Function The uksort() function in PHP sorts an array by keys using a user-defined comparison function. Syntax uksort ( array &$array , callable $key_compare_func ) Parameter array(required)- This parameter represents the input array. key_compare_func(required)- This parameter represents a string that describe a callable comparison function....

1 minute read.

PHP is_numeric() Function

The is_numeric() function in PHP finds whether the specified variable is a number or a numeric string. It returns a Boolean value TRUE if the specified var is a number or a numeric string else it returns FALSE otherwise. Syntax is_numeric ( mixed...

1 minute read.

PHP Indexed Array

In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of...

3 minutes read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP array_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP array_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP array_merge() Function

PHP array_merge() Function The array_merger() Function in PHP merges the elements of one or more arrays wherein the values of one are appended to the end of the previous one. It returns the resulting array...

1 minute read.

PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. Syntax array_key_last ( array $array )  Parameter array(required)- This parameter signifies the input array. Return This function returns the last key of the...

1 minute 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 sprintf() Function

PHP sprintf() Function The sprintf() function in PHP is used to write a formatted string to a variable and returns a formatted string. PHP 4 and above versions support the sprintf() function. The related functions are...

8 minutes read.

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

PHP array_column() Function

PHP array_column() Function The array_column() function in PHP is used to return the values from a single column specified in the input array. Syntax array_column (array $input, mixed $column_key [, mixed $index_key]) Parameter input(required)- This parameter represents a multi-dimensional array or an array of...

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.

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 array_flip() function

PHP array_flip() function The array_flip() function in PHP exchanges all keys with their associated values in an array and returns an array in flip order. If the specified value has several occurrences, the latest key will be...

1 minute read.

PHP strstr() Function

PHP strstr() Function The strstr() function in PHP finds the first occurrence of a string. It returns part of the parameter haystack string starting from and including the first occurrence of needle to the end of the haystack parameter. This function is case-sensitive.  Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] )  Parameter  haystack(required)-...

2 minutes read.