What is the best practice for separating output and processing in PHP functions to ensure proper display in HTML elements?

When separating output and processing in PHP functions for proper display in HTML elements, it is best practice to have the processing logic within the function and return the processed data. Then, the output should be handled separately in the HTML document using PHP tags to echo the returned data.

// Function to process data
function processData($data) {
    // Processing logic here
    return $processedData;
}

// Call the function and store the processed data
$processedData = processData($data);

// Output the processed data in HTML
echo "<div>$processedData</div>";