How to Use Global Variables Inside Header and Footer

In functions.php you can create a constant with the user id, like this.

// if somehow this constant exists this will prevent a php error
if (!defined('CURRENT_USER_ID')) {
    // if user is not logged it this constant will contain 0
    define('CURRENT_USER_ID', get_current_user_id());
}

Now you have a constant variable available anywhere in your theme.

To access it you can simply call it, for example

echo CURRENT_USER_ID;