PHP print_r() Function
Definition
print_r() function takes one parameter and outputs information about a variable, such as its type, length, and contents.
Syntax
PHP print_r() Function has the following syntax.
mixed print_r ( mixed $expression [, bool $return = false ] )
Parameter
Parameter | Description |
---|---|
expression | The expression to be printed. |
return | TRUE value enable print_r() return the information rather than print it. |
Return
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.
Example
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//from ww w . j a v a 2s. c o m
$myarray = array("PHP", "Java", "Python","java2s.com");
$size = count($myarray);
$output = print_r($myarray, true);
print $output;
?>
The code above generates the following result.