What is print_r() in PHP?
The print_r() function in PHP is used to print human-readable information about a variable. It is particularly useful for debugging purposes when you want to display the structure and contents of arrays and objects.
 Syntax of print_r() in PHP
print_r(variable, return);
Parameters:
- variable(Required) – The variable (array, object, etc.) you want to print.
- return(Optional, default = false) – If set to- true, the output is returned as a string instead of being printed directly.
 Key Features of print_r()
- Displays values in a structured and readable format.
- Works best for arrays and objects.
- Can be used with echoor stored in a variable if the second parameter istrue.
🧑💻 Examples of print_r() in PHP
Example 1: Using print_r() with an Array
<?php
$fruits = array("Apple", "Banana", "Orange");
print_r($fruits);
?>
Output:
Array
(
    [0] => Apple
    [1] => Banana
    [2] => Orange
)
Example 2: Using print_r() with an Associative Array
<?php
$user = array("name" => "John", "age" => 30, "email" => "john@example.com");
print_r($user);
?>
Output:
Array
(
    [name] => John
    [age] => 30
    [email] => john@example.com
)
Example 3: Storing print_r() Output as a String
<?php
$data = array("PHP", "MySQL", "JavaScript");
$output = print_r($data, true);
echo "<pre>$output</pre>";  // Useful for formatting in HTML
?>
 When to Use print_r():
- While debugging arrays or objects.
- When checking the contents of a variable.
- During development (avoid using in production output).
 Limitations of print_r()
- Not ideal for complex nested structures (use var_dump()instead for detailed types).
- Not suitable for formatted frontend output — meant for backend debugging only.