Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matttonks11's avatar

Checking if session variables are empty

if(empty(session('access_token')) || empty(session('refresh_token')) || empty(session('token_expires'))) {
            return '';
        }

Is there a cleaner way to write this? are there any useful functions from the session helper that can be used?

0 likes
2 replies
BishoyWagih's avatar

you can use session() helper function to check for a key in session..

if(session()->has('access_token')) { return ''; }

1 like
ctoma's avatar
ctoma
Best Answer
Level 2

As @BishoyWagih mentioned there is a has() function, but it also accepts an array. So you could write it as:

if (!session()->has(['access_token', 'refresh_token', 'token_expires'])) { 
    return ''; 
}
1 like

Please or to participate in this conversation.