print_r


php128 apg

PRINTS human-readable information about a variable.



<?php

 mix print_r 
mix $expression [, bool $return FALSE ] )

where,

$expression The expression to be printed

$return 
To control if the information will be printed or not
                 
SEE the below TABLE )

?>



This function returns human-readable information about a variable and, specially designed to print ARRAYS and/or OBJECTS.


ARRAYS, RESOURCES and OBJECTS ...

... will be studied in more detail ...


... in a special chapter of this tutorial



 $expression 


The expression to be printed.



$return

VALUE WHAT DOES
FALSE The information will be printed
TRUE The information will not be printed
ed48


  1 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
      SOME USER-DEFINED ARRAY EXAMPLES
      COMMON UNIDIMENSIONAL
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  
$arr01a = array(28183232188);
print_r($arr01a);
echo 
'<br><br>';

$arr01b = [ 2818323218];
print_r($arr01b);
echo 
'<br><br>'

$arr01c = [ 28'18'32'THIRTY-TWO'"eighteen"];
print_r($arr01c);
echo 
'<br><br>';

$arr01d = [ 2997924586.67428E-11'c'"G" ];
print_r($arr01d);
echo 
'<br><br>';

$arr01e = [010230B00110x20b ];
print_r($arr01e);
echo 
'<br><br>';

?>

 RESULT   

$arr01a =
Array ( [0] => 2 [1] => 8 [2] => 18 [3] => 32 [4] => 32 [5] => 18 [6] => 8 )

$arr01b =
Array ( [0] => 2 [1] => 8 [2] => 18 [3] => 32 [4] => 32 [5] => 18 [6] => 8 )

$arr01c =
Array ( [0] => 2 [1] => 8 [2] => 18 [3] => 32 [4] => THIRTY-TWO [5] => eighteen [6] => 8 )

$arr01d =
Array ( [0] => 299792458 [1] => 6.67428E-11 [2] => c [3] => G )

$arr01e =
Array ( [0] => 531 [1] => 3 [2] => 523 )


  2 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        SOME USER-DEFINED ARRAY EXAMPLES
        UNIDIMENSIONAL ASSOCIATIVE
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr02a = array('K' => 2"L" => 8"M" => 18,
"N" => 32"O" => 32"P" => 18"Q" => 8);

print_r($arr02a);
echo 
'<br><br>';

$arr02b = [ 'K' => 2"L" => 8"M" => 18,
"N" => 32"O" => 32"P" => 18"Q" => ];

print_r($arr02b);
echo 
'<br><br>';

$arr02c = [ 'THIRTY-TWO' => 32"EIGHTEEN" => 18 ];

print_r($arr02c);
echo 
'<br><br>';

$arr02d = [ 'c' => 299792458'G' => 6.67428E-11 ];
print_r($arr02d);
echo 
'<br><br>';

$arr02e = [ => 'two'=> 'four' ];
print_r($arr02e);
echo 
'<br><br>';

?>

 RESULT   

$arr02a =
Array ( [K] => 2 [L] => 8 [M] => 18 [N] => 32 [O] => 32 [P] => 18 [Q] => 8 )

$arr02b =
Array ( [K] => 2 [L] => 8 [M] => 18 [N] => 32 [O] => 32 [P] => 18 [Q] => 8 )

$arr02c =
Array ( [THIRTY-TWO] => 32 [EIGHTEEN] => 18 )

$arr02d =
Array ( [c] => 299792458 [G] => 6.67428E-11 )

$arr02e =
Array ( [2] => two [4] => four )


  3 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
       SOME USER-DEFINED ARRAY EXAMPLES
       COMMON MULTIDIMENSIONAL
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 

$arr03a = array(281832, array (32188));

print_r($arr03a);
echo 
'<br><br>';

$arr03b = [ 281832, [ 3218] ];

print_r($arr03b);
echo 
'<br><br>';

$arr03c = [ 28'18'32, [ 'THIRTY-TWO'"eighteen"] ];

print_r($arr03c);
echo 
'<br><br>';

$arr03d = [ 2997924586.67428E-11, [ 'c'"G" ] ];

print_r($arr03d);
echo 
'<br><br>';

$arr03e = [ ['01023''0B0011''0x20b'],
[
'octal''binary''hexadecimal' ] ];

print_r($arr03e);
echo 
'<br><br>';

$arr03f = ["Countries""Continents",
[
"Brasil""Portugal""Japan"],
[
"South America""Europe""Asia"] ];

print_r($arr03f);
echo 
'<br><br>';

?>

 RESULT   

$arr03a =
Array ( [0] => 2 [1] => 8 [2] => 18 [3] => 32 [4] => Array ( [0] => 32 [1] => 18 [2] => 8 ) )

$arr03b =
Array ( [0] => 2 [1] => 8 [2] => 18 [3] => 32 [4] => Array ( [0] => 32 [1] => 18 [2] => 8 ) )

$arr03c =
Array ( [0] => 2 [1] => 8 [2] => 18 [3] => 32 [4] => Array ( [0] => THIRTY-TWO [1] => eighteen [2] => 8 ) )

$arr03d =
Array ( [0] => 299792458 [1] => 6.67428E-11 [2] => Array ( [0] => c [1] => G ) )

$arr03e =
Array ( [0] => Array ( [0] => 01023 [1] => 0B0011 [2] => 0x20b ) [1] => Array ( [0] => octal [1] => binary [2] => hexadecimal ) )

$arr03f =
Array ( [0] => Countries [1] => Continents [2] => Array ( [0] => Brasil [1] => Portugal [2] => Japan ) [3] => Array ( [0] => South America [1] => Europe [2] => Asia ) )


  4 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
         SOME USER-DEFINED ARRAY EXAMPLES
         ASSOCIATIVE MULTIDIMENSIONAL
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr04a = array ("Countries" => array("Brasil"
                                                           
"Portugal"
                                                           
"Japan"),
"Continents" => array ("South America"
                                   
"Europe"
                                   
"Asia"));

$arr04b = [ "Countries" => [ "Brasil"
                                           
"Portugal"
                                           
"Japan" ],
"Continents" =>  ["South America"
                          
"Europe"
                          
"Asia"]];

print_r($arr04a);

echo 
'<br><br>';

print_r($arr04b);


$arr04c = [ "Countries" => [ 'BRA' => "Brasil"
                                         
'POR' => "Portugal"
                                         
'JPN' => "Japan" ],
           
"Continents" =>  ['SA' => "South America"
                                     
'EU' => "Europe"
                                     
'AS' => "Asia"] ];
                                     
echo 
'<br><br>';

print_r($arr04c);

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   PAY ATTENTION ON THE NEXT TWO EXAMPLES
   print_r is set with bool $return = TRUE, (true)
   
   No data is printed because $return was set 
   to TRUE or true - however, all information 
   on ARRAY elements is returned without problems
   
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$arr04d = [ "Countries" => [ "Brasil"
                                           
"Portugal"
                                           
"Japan" ],  
           
"Continents" =>  ["South America"
                                     
"Europe"
                                     
"Asia"] ];

echo 
'<br><br>';                                     
  
print_r($arr04dTRUE);   

 
$arr04e = [ "Countries" => ['BRA' => "Brasil"
                                       
'POR' => "Portugal"
                                       
'JPN' => "Japan" ],   
           
"Continents" =>  ['SA' => "South America"
                                    
'EU' => "Europe"
                                    
'AS' => "Asia"] ];

echo 
'<br><br>';                                    
   
print_r($arr04etrue);   

?>

 RESULT   

$arr04a =
Array ( [Countries] => Array ( [0] => Brasil [1] => Portugal [2] => Japan ) [Continents] => Array ( [0] => South America [1] => Europe [2] => Asia ) )

$arr04b =
Array ( [Countries] => Array ( [0] => Brasil [1] => Portugal [2] => Japan ) [Continents] => Array ( [0] => South America [1] => Europe [2] => Asia ) )

$arr04c =
Array ( [Countries] => Array ( [BRA] => Brasil [POR] => Portugal [JPN] => Japan ) [Continents] => Array ( [SA] => South America [EU] => Europe [AS] => Asia ) )



No data is printed because $return was set to TRUE or true - however, all information on ARRAY elements is returned without problems:

$arr04d =

$arr04e =

See the next exercise.


  5 EXERCISE   

<?php

$arr05a 
= [ "Countries" => [ "Brasil"
                                          
"Portugal"
                                          
"Japan" ], 
              
"Continents" =>  ["South America"
                                         
"Europe"
                                         
"Asia"]];
 
print_r($arr05aTRUE);  
echo 
'<br><br>';

$arr05b 
  [ 
"Countries" => [ 'BRA' => "Brasil"
                            
'POR' => "Portugal"
                            
'JPN' => "Japan" ],  
    
"Continents" =>  ['SA' => "South America"
                              
'EU' => "Europe"
                              
'AS' => "Asia"] ];

print_r($arr05atrue);  
echo 
'<br><br>';

print_r($arr05b['Countries']);
echo 
'<br><br>';
print_r($arr05b['Continents']);

?>

 RESULT   

In either case, no data will be printed.
Only the information returned is distributed without being displayed.

print_r is set with $return = TRUE (true).

In this example we have the ARRAYS elements available:

Array ( [BRA] => Brasil [POR] => Portugal [JPN] => Japan )
and
Array ( [SA] => South America [EU] => Europe [AS] => Asia )


  6 EXERCISE   

<?php 

$str06a 
'Lorem ipsum inceptos mollis eros risus ut luctus
 taciti, morbi viverra lorem et phasellus in vitae ultrices,
 neque torquent at condimentum felis vel nullam.'
;

$nbr06a 1.23454;

print_r($str06a);

echo 
'<br><br>';

print_r($nbr06a);

?> 

 RESULT   

Lorem ipsum inceptos mollis eros risus ut luctus taciti, morbi viverra lorem et phasellus in vitae ultrices, neque torquent at condimentum felis vel nullam.

1.23454



  7 EXERCISE   

<?php 

$v07a 
5.455_052e-10;
// float
$v07b 300_482_381;
// decimal
$v07c 0xBBFF_3E48;
// hexadecimal
$v07d 0b0101_1111;
// binary

// Representations available since the PHP 7.4.0

print_r ('$v07a = ' $v07a);

print_r ('<br><br>$v07b = ' $v07b);

print_r ('<br><br>$v07c = ' $v07c);

print_r ('<br><br>$v07d = ' $v07d);

?> 

 RESULT   

$v07a = 5.455052E-10

$v07b = 300482381

$v07c = 3154067016

$v07d = 95


It does not matter what the base of the VARIABLE was originally defined, as the result will always be expressed in base 10.