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 Do While Loop

PHP do while loop is used to execute the code at least one time because condition is checked after executing the code. Syntax: do{ //code to be executed  }while(condition); Output <?php $n=1;  do{ echo "$n<br/>";  $n++;  }while($n<=5);  ?> Output 1 2 3 4 5 ← Prev Next →...

1 minute read.

PHP Validation

PHP validation is used to check whether the field is filled or not in the proper way by the user. There are two types of validation in PHP. Client Side Validation:...

5 minutes read.

PHP nl_langinfo() Function

PHP nl_langinfo() Function The nl_langinfo() function in PHP returns specific local information. This function does not work on Windows platforms. Syntax nl_langinfo ( int $item ) Parameter Item(required)- This parameter specifies the  integer value of the element or the constant name of the...

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

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

1 minute read.

PHP is_string() Function

PHP is_string() Function The is_ string() function in PHP finds whether a variable is a string or not. It returns a Boolean value TRUE if the parameter var is string, else it returns FALSE. Syntax is_string ( mixed $var ) Parameter var(required)- This parameter represents...

1 minute read.

PHP sizeof() Function

PHP sizeof() Function The sizeof() function in counts all elements and return the size of the array. This function is an alias of count(). Syntax sizeof( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)-...

1 minute read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

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

PHP array_pop() Function The array_pop() function in PHP pops or removes the last element from the array shortening the array by one element. Syntax array_pop ( array &$array ) Parameter array(required)- This parameter represents the input array to get the value...

1 minute read.

Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. PHP does...

3 minutes read.

PHP array_fill() function

PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...

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

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.

PHP is_long() Function

The PHP is_long () function in PHP is used to find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

PHP addcslashes() Function

The addcslashed() function in PHP is used to add quote string with slashes in a C style. It is case-sensitive function.  Syntax addcslashes (str, charlist ) Parameter str(required)- This parameter represents the string to be escaped. charlist(required)-...

1 minute read.

PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays. Syntax array_intersect ( array $array1 , array $array2 [, array $... ]) Parameter array1(required)- This parameter...

1 minute read.

PHP strlen() Function

PHP strlen() Function The strlen() function in PHP is used to return the length of the specified string. Syntax strlen ( string $string ) Parameter string(required)- This parameter represents the string which is measured for length. Return This function returns the length of the string on success or returns 0 if the specified...

1 minute read.

PHP array_diff_assoc() Function

PHP array_diff_assoc() Function The array_diff_assoc() Function in PHP compares the difference of arrays and returns an array containing all the values from array1 that are not present in any of the other arrays. Syntax array_diff_assoc ( array $array1 , array $array2 [, array $... ] ) Parameter array1(required)- This parameter signifies...

1 minute read.