How can PHP code be optimized to ensure clean and effective output formatting on a webpage?

To optimize PHP code for clean and effective output formatting on a webpage, it is important to separate the PHP logic from the HTML presentation. This can be achieved by using PHP to generate data and then passing it to the HTML template for display. Additionally, using functions like `echo` and `printf` for outputting content can help maintain readability and organization in the code.

<?php
// PHP logic to generate data
$data = "Hello, World!";

// HTML template for displaying data
?>
<!DOCTYPE html>
<html>
<head>
    <title>Optimized PHP Output</title>
</head>
<body>
    <h1><?php echo $data; ?></h1>
</body>
</html>