How can PHP developers optimize the code provided in the forum thread to prevent layout issues caused by the pagination function?

The issue with the current code is that the pagination function is not taking into account the layout of the page, causing layout issues when navigating between pages. To optimize the code and prevent layout issues, PHP developers can implement a solution where the pagination function also includes the necessary CSS classes or styles to maintain the layout consistency.

// Example code snippet to optimize pagination function with CSS classes for layout consistency

// Pagination function with added CSS classes for layout consistency
function custom_pagination($total_pages, $current_page) {
    echo '<div class="pagination">';
    for ($i = 1; $i <= $total_pages; $i++) {
        if ($i == $current_page) {
            echo '<span class="current_page">' . $i . '</span>';
        } else {
            echo '<a href="?page=' . $i . '" class="page_link">' . $i . '</a>';
        }
    }
    echo '</div>';
}