How can PHP developers ensure that generated HTML is properly formatted and indented?
PHP developers can ensure that generated HTML is properly formatted and indented by using PHP's built-in DOMDocument class. This class allows developers to create a new HTML document, manipulate its structure, and output the formatted HTML. By utilizing methods like `formatOutput(true)` and `preserveWhiteSpace(false)`, developers can ensure that the generated HTML is correctly formatted and indented.
<?php
// Create a new DOMDocument object
$dom = new DOMDocument();
// Load the HTML content into the DOMDocument
$dom->loadHTML($html_content);
// Set the formatting options
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
// Output the formatted HTML
echo $dom->saveHTML();
?>