What is the best way to capture the output of print_r or var_dump in PHP and send it via email?

When you want to capture the output of print_r or var_dump in PHP and send it via email, you can use output buffering to capture the output and then send it as the body of an email using the mail function. This way, you can easily send the debug information to yourself or another recipient for troubleshooting purposes.

// Start output buffering
ob_start();

// Your debug code here (e.g. print_r or var_dump)
$data = ['example' => 'data'];
print_r($data);

// Get the contents of the output buffer
$output = ob_get_clean();

// Set up email parameters
$to = 'recipient@example.com';
$subject = 'Debug Information';
$message = $output;
$headers = 'From: sender@example.com';

// Send the email
mail($to, $subject, $message, $headers);