Variable Scope
Variable scope is a tricky but important concept. Every variable in PHP has a scope to it, which is to say a realm in which the variable (and therefore its value) can be accessed. For starters, variables have the scope of the page in which they reside. So if you define $var, the rest of the page can access $var, but other pages generally cannot (unless you use special variables).
Since included files act as if they were part of the original (including) script, variables defined before the include() line are available to the included file (as you’ve already seen with $page_title and header.html). Further, variables defined within the included file are available to the parent (including) script after the include() line.
All of this ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access