You’d have to declare it first above those includes and globalize it within header or footer before getting or setting the value.
however- in the specific context you’re speaking of- getting a custom field value, it’s only retrieved from the database on the first call, then cached, so subsequent calls won’t hit the database again.