What are the best practices for structuring PHP code to ensure proper display of tabular data with numerical ordering?

When displaying tabular data with numerical ordering in PHP, it's essential to structure the code properly to ensure the data is displayed correctly. One best practice is to separate the data retrieval and display logic by using functions or classes. Additionally, sorting the data before displaying it can help ensure numerical ordering is maintained.

<?php
// Sample array of numerical data
$data = [5, 2, 8, 1, 10];

// Function to sort data numerically
function sortNumerically($a, $b) {
    return $a - $b;
}

// Sort the data numerically
usort($data, 'sortNumerically');

// Display the data in a table
echo '<table>';
foreach ($data as $value) {
    echo '<tr><td>' . $value . '</td></tr>';
}
echo '</table>';
?>