PHP print_r() Function
In this chapter you will learn:
- Definition for PHP print_r() Function
- Syntax for PHP print_r() Function
- Parameter for PHP print_r() Function
- Return for PHP print_r() Function
- Example - print out for variable
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// j a v a 2 s . com
$myarray = array("PHP", "Java", "Python","java2s.com");
$size = count($myarray);
$output = print_r($myarray, true);
print $output;
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP printf() Function
- Syntax for PHP printf() Function
- Parameter for PHP printf() Function
- Additional format for PHP printf() Function
- Padding the Output
- Specifying Number Precision
- Swapping Arguments
- Note for PHP printf() Function
- Return for PHP printf() Function
- Example - Output string with printf
- Example - Output more than one value with printf
- Example - Set value type in printf
- Example - Output escaped %
- Example - Float value rounding
- Example - Format two values
- Example - Use of placeholders
- Example - Specifying Signs