PHP print_r() Function

The PHP print_r() function is used to print information of specified variable, looks more readable. For example:

   $x = array("Berlin", "London", "NewYork", "Munich");

The output of above PHP example on print_r() function is:

php print_r function

See the output of $x, an array variable. We can clearly see that, at index 0, the Berlin is stored. Similarly at index 1, the London is stored, and so on. That is, if you use the following PHP statement:

echo $x[0];

Then you will get Berlin as output.

PHP print_r() Syntax

The syntax of print_r() function in PHP, is:

print_r(variable, return)

The first parameter (variable) is required, whereas the second parameter (return) is optional.

Note - The variable parameter is used to specify the variable, of which we need to print the information about.

Note - The default value of return parameter is false. But if we specify this parameter as true, then the function print_r() returns the information, without printing.

Note - If the variable parameter is an integer, float, or a string type value, then the function print_r() prints the value itself. Whereas if the variable parameter is an array or an object, then it returns the information about the variable in the form of keys and elements.

PHP Online Test

« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋