The print_r() function is a built-in function in PHP that prints a human-readable representation of a variable to the output. It is commonly used for debugging purposes to inspect the contents of a variable.


The syntax of the print_r() function is as follows:

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

The function takes one or two parameters. The first parameter, $var, represents the variable to be printed. The second parameter, $return, is an optional boolean parameter that determines whether the function should return the output as a string instead of printing it to the output. If $return is true, the function returns the output as a string. Otherwise, it prints the output to the output buffer.

Example Usage

Here is an example of how to use the print_r() function in PHP:

$array = ["apple", "banana", "cherry"];

In this example, we define an array $array containing three elements. We use the print_r() function to print the contents of the array to the output. The output shows the contents of the array in a human-readable format:

    [0] => apple
    [1] => banana
    [2] => cherry


The print_r() function is a useful tool for printing the contents of a variable to the output in a human-readable format. It can be used for debugging purposes or for displaying data to users. By using this function, developers can inspect the contents of a variable and ensure that their code is working with the expected data.

Practice Your Knowledge

What does the print_r() function in PHP do?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?