Printing variables in PHP using print_r

The print_r function in PHP allows you to print the contents of variables, arrays, and objects in a human-readable format. Primary print_r is used for debugging since it lets you visually inspect data structures' contents. To use print_r, pass the variable you want to inspect as the first argument to the function. If you want the output returned as a string rather than printed to the screen, specify true as the second argument to the function. In this PHP print_r example, we print the contents of a variable on the screen and save it to a string. Click Execute to run the PHP print_r example online and see the result.
Printing variables in PHP using print_r Execute
echo 'Directly print the array to the screen using print_r';
$data = array(
    'name' => 'John Smith',
    'age' => 35,
    'location' => 'New York'

echo 'Capture the output as a string and then print it';
$output = print_r($data, true);
echo $output;
Updated: Viewed: 77 times