Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames Eaves
1,376 PointsWhy does page title have to be defined before the "include" command?
Hi, Can someone explain to me why the page title have to be defined before the "include" command? Thank you! James
4 Answers
James Eaves
1,376 PointsI think I understand (I just rewatched the video). The point is to have PHP generate a page title automatically for that page. Thus in order to refer to that title in the Include file, I have to declare what the page title is before the include file is executed.
Thank you for your time! James
Aaron Mannino
3,767 PointsPretend your include is just that straight code, instead of being an include (because that's what "including" does; anything in that included file is included just exactly as it is in the file, right where you put the include). If it was just the straight code, it might be like this:
<?php $pageTitle = "Contact Mike"; ?>
<?php $echo pageTitle; ?>
If those two code blocks were reversed, you'd be trying to "echo" the value of variable that hadn't been defined yet. First, it must be defined, in order for its value can be called upon later on in the code.
Let's go back to this example, but using the include:
<?php $pageTitle = "Contact Mike"; ?>
<?php include("inc/header.php"); ?>
Make more sense?
Aaron Munoz
11,177 PointsIt's like giving you water before giving you a bucket. You want to declare your variable first so that the pages loaded afterwards will know what that variable is. (Just thought I'd give a brief answer).
Michael Tigue
4,799 PointsThe code is executed in order from top to the bottom. If the variable is declared after the include file, the include file won't know what the variable is because the include file code was run before the variable was declared.