What are some best practices for optimizing PHP code for displaying calendar data?

When displaying calendar data in PHP, it's important to optimize the code to ensure efficient performance. One way to do this is by minimizing database queries and optimizing loops to reduce the number of iterations. Additionally, caching the calendar data can help improve load times and overall user experience.

// Example of optimizing PHP code for displaying calendar data

// Assume $calendarData is an array containing the calendar data

// Cache the calendar data to reduce database queries
if (!isset($calendarData)) {
    $calendarData = fetchCalendarDataFromDatabase();
}

// Loop through the calendar data efficiently
foreach ($calendarData as $event) {
    // Display each event
    echo $event['title'] . ' - ' . $event['date'] . '<br>';
}