PHP Build a Basic PHP Website (2018) Building a Media Library in PHP Adding Active States to the Navigation

Please explain why the index page needs to have the $section = null;

If we load the index.php page and comment out "$section.php = null;" why are all of the links underlined?

Shouldn't it still include/load the header.php file and find that

<li class="books<?php if ($section == "books") {echo " on";} ?>"><a href="catalog.php?cat=books">Books</a></li>

is false? Because the $_GET["cat"] was never set from the index.php file.

Therefore it shouldn't append the " on" class correct? Yet by default it underlines the links? Maybe there is something I am missing within the CSS file but I removed the php script from the list items and it then removed all of the underlines.


1 Answer

Seth Kroger
Seth Kroger
56,385 Points

When you comment out the $section = null; in index.php, when you get to the header $section will still be undeclared/undefined. Because it's undefined, it causes an error when you try to access it. What happens with the error depends on the server's settings. On Workspaces you'll get this output:

<li class="books<br />
<b>Notice</b>:  Undefined variable: section in <b>/home/treehouse/workspace/includes/header.php</b> on line <b>15</b><br />

Note that the error message includes the word "on".

Thats kind of what I kind of figured. I assumed if I could set the server side settings it would have thrown a warning or something. It just seemed odd that when an undefined variable is compared that it would automatically set the comparison to true.