Passing Variables to Snippets

Let's learn how to change the page title dynamically by introducing a title variable.

We'll cover the following

Changing page title dynamically

There’s one problem: if we include _header.php everywhere, every page will get the same title, “Login”. To avoid this, we should make the title dynamic by introducing a $title variable. Every page can provide its own title by setting this $title variable before including _header.php. This variable will then be available inside _header.php. Just in case the page script doesn’t set this variable, we should also provide a sensible default value. In _header.php, the code should look like this:

Get hands-on with 1200+ tech skills courses.