PHP Tutorial - PHP print_r() Function


print_r() function takes one parameter and outputs information about a variable, such as its type, length, and contents.


PHP print_r() Function has the following syntax.

mixed print_r ( mixed $expression [, bool $return = false ] )


expressionThe expression to be printed.
returnTRUE value enable print_r() return the information rather than print it.


If given a string, integer or float, the value itself will be printed.

If given an array, values will be presented in a format that shows keys and elements.

When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE.


In case of arrays, print_r() iteratively outputs all elements inside the array.

If the script is running through a web browser not from the command line, put a HTML <pre> tag before your print_r() calls.

We can provide a second parameter as 'true' to print_r(), and make print_r() return value, and not print anything out.

$myarray = array("PHP", "Java", "Python",""); 
$size = count($myarray); 
$output = print_r($myarray, true); 
print $output; 

The code above generates the following result.

Example 2

The following code shows how to read a file into an array.


The code above generates the following result.