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 ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.