What are some best practices for separating data operations from HTML output in PHP?

Separating data operations from HTML output in PHP is a best practice to improve code readability, maintainability, and reusability. One common way to achieve this separation is by using a template engine like Twig or Smarty to handle the presentation layer while keeping data manipulation logic in separate PHP files or classes.

// data_operations.php

function fetchData() {
    // Data retrieval logic here
    return $data;
}

// index.php

require 'data_operations.php';

$data = fetchData();

// Use a template engine like Twig to render HTML output