Based upon @toscho snippet, here's a real life example. Assuming that you don't need to access a var directly from inside the template file.
function wpse_69365_var_storage()
{
static $internal;
if ( 'wp_head' === current_filter() )
{
if ( /* Condition is met */ )
return $internal = 4; // Set to what you need.
return 'default_value';
}
return print $internal;
}
// Call in header.php:
add_action( 'wp_head', 'wpse_69365_var_storage' );
// Call in footer.php:
add_action( 'wp_footer', 'wpse_69365_var_storage' );