What best practices should be followed when using PHP to manipulate data for printing purposes?

When using PHP to manipulate data for printing purposes, it is important to properly sanitize and format the data to ensure it is displayed correctly and securely. This includes escaping special characters to prevent SQL injection and cross-site scripting attacks, as well as formatting the data in a way that is easy to read and visually appealing.

// Example of sanitizing and formatting data for printing
$data = "<script>alert('XSS attack!');</script>";
$escaped_data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
$formatted_data = nl2br($escaped_data);

echo $formatted_data;