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 ] )
Parameter | Description |
---|---|
expression | The expression to be printed. |
return | TRUE 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.
<?PHP
$myarray = array("PHP", "Java", "Python","java2s.com");
$size = count($myarray);
$output = print_r($myarray, true);
print $output;
?>
The code above generates the following result.
The following code shows how to read a file into an array.
<?php
print_r(file("test.txt"));
?>
The code above generates the following result.