How can one optimize the onlineCheck function in the provided PHP script for better performance?

The onlineCheck function in the provided PHP script can be optimized for better performance by reducing the number of unnecessary database queries. One way to achieve this is by storing the result of the online status check in a session variable and updating it only when necessary, instead of querying the database every time the function is called.

// Optimized onlineCheck function
function onlineCheck() {
    if(isset($_SESSION['online_status'])) {
        return $_SESSION['online_status'];
    } else {
        // Perform the online status check query here
        $online = // Query to check online status
        
        $_SESSION['online_status'] = $online;
        return $online;
    }
}