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 trial

PHP

I am not understanding the priority of a task in the Build a basic PHP website. could anyone explain in detail?

Hey there I am currently working on the Build a basic PHP website and have stumbled across something I do not understand. I am currently up to the stage where I am using the catalog.php page to be able to display Movies, Books or Music, depending what link is clicked on the header. I am slightly confused by the code priority it has been implemented in and curious to know why it still works.

In the project video it states putting the code in the following order:

if ($_GET["cat"] == "books"){
  $pageTitle = "Books";
  $section = "books";
} else if ($_GET["cat"] == "movies") {
  $pageTitle = "Movies";
  $section = "movies";
} else if ($_GET["cat"] == "music") {
  $pageTitle = "Music";
  $section = "music";
}

include("inc/header.php"); 

It is this order which is confusing me. It is my understanding that the $_GET["cat"] code is obtaining what was placed in the links of the header.php file. It is checking to see if "cat" equals either books, music or movies and then changing the page to meet the needs of the IF statement.

However my question is this: How is it doing this? Because shouldn't the include("inc/header.php"); go before the above If statement so it knows what link has been clicked upon and then can show the relevant section of either books, music or movies? How comes it still works as surely it should be loaded before this IF statement is executed?

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

As I understand it the header.php is the file that contains the links to the CSS, page title information and references to variables included in catalog.php, but initialised at the very top of the file. The if statement in your code is what is assigning values, but there will be code in header.php or another file that displays the relevant value. :-)

$_GET is a super global; it's always available. It's not in header.php, if that's what you were thinking?

The value of $pageTitle is worked out 1st (using super global value), then used in header.php.

If you included header.php 1st, you would get an error about unassigned variables or similar.