How can one avoid or troubleshoot issues related to recursion when using print_r in PHP?

When using `print_r` in PHP to print out arrays or objects, it can lead to issues related to recursion if the data structure contains circular references. To avoid this problem, you can use the `json_encode` function to convert the array or object to a JSON string before printing it. This will handle circular references more gracefully.

// Avoiding recursion issues with print_r using json_encode

$data = [
    'name' => 'John',
    'age' => 30
];

// Convert the array to a JSON string before printing
echo json_encode($data, JSON_PRETTY_PRINT);