How can the use of empty() function in PHP help simplify session variable checks for login functionality?

When checking session variables for login functionality in PHP, you often need to check if a variable is set and not empty. Using the empty() function can simplify this process by checking both conditions in one step. This can make the code cleaner and more efficient.

// Check if the session variable 'user_id' is set and not empty
if (!empty($_SESSION['user_id'])) {
    // User is logged in, perform necessary actions
    echo "Welcome, User!";
} else {
    // User is not logged in, redirect to login page
    header("Location: login.php");
    exit();
}