- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Learn how to use PHP print_r function
Introduction
In this PHP Tutorial, I'm Publishing some information about "PHP print_r() function", which includes Syntax, Examples, Usage, Related functions and Some Details associated with it. "print_r" Function is a Member of Variable Handling Functions in PHP. This type/category of Functions basically deals with data stored in and associated with variables, whether, Variable is an array, String or an Object.
Definition of print_r Function
The PHP print_r function is used to display data stored in an array on screen i.e. Structured Visual representation of array data in an Human Readable form which is shown below.
Sample Function Output - Visual Representation
Array ( [a] => My [b] => Name [c] => Array ( [0] => Is [1] => itech ) )
Here, Below is the syntax of PHP print_r function in which $expression will accept array as INPUT type and $return will accept either "true" or "false" as INPUT.
Reference Notes :
- If you'll give string, integer or float as input type instead of array in $expression, then, VALUE itself will be printed.
- If $return is set to "true" then "print_r" function will return an output instead of printing it directly on the screen. You can do so, if you want to save returned output in form of variable value.
Syntax of print_r Function
mixed print_r ( mixed $expression [, bool $return = false ] );
Usage and Examples
Here, Below is an basic example showing the usage of "print_r" function which includes both PHP Code (PHP Script) and Screen Output. We are creating two type of arrays
- Simple Array,
- And Multidimensional Array,
Then, We are printing both of them on screen using print_r function directly. After that, We are creating two more variable $arrayS and $arrayMD, which shows the Usage of storing output in the form of variables and then printing it via PHP echo statement.
PHP Script
<?php $array1=array( "a" => "Feels Good", "b" => "to be a", "c" => "Programmer" ); $array2=array( "a" => "I AM" , "b" => array( "c" => "NOT SO GOOD" , "d" => array( "e" => "IN PROGRAMMING" ) ) ); print_r($array1); print_r($array2); //Below we are storing the print_r output in the form of variables. $arrayS=print_r($array1, true); $arrayMD=print_r($array2, true); //Now, We are outputting the variables echo $arrayS; echo $arrayMD; ?>
Output of Above PHP Code
Array ( [a] => Feels Good [b] => to be a [c] => Programmer ) Array ( [a] => I AM [b] => Array ( [c] => NOT SO GOOD [d] => Array ( [e] => IN PROGRAMMING ) ) ) ----------------------------------------------------------- Array ( [a] => Feels Good [b] => to be a [c] => Programmer ) Array ( [a] => I AM [b] => Array ( [c] => NOT SO GOOD [d] => Array ( [e] => IN PROGRAMMING ) ) )
Related PHP Functions
- ob_start() - Turn on output buffering,
- var_dump() - Dumps information about a variable,
- var_export() - Outputs or returns a parsable string representation of a variable,
- Echo () PHP Function - Output one or more strings,
- Print () PHP Function - Output a string,
-
Array () PHP Function - Creates an ARRAY.