A PHP Template System | 
We're only going to scratch the surface of the conditional logic available in PHP, but it will still give us a powerful tool to work with. Let's say that you have a design in mind for your web site that has a sidebar on some pages but not others.
An efficient way to handle this would be to create a variable that designates a page as either having or not having a sidebar. We could call the variable $includeSidebar (remember that spaces are not allowed in variable names, so I chose to run the words together; I could have used a dash or an underscore instead). On each page, we would have a line of code that could look like this:
$includeSidebar = 'yes';
or like this:
$includeSidebar = 'no';
Then, in the template, we could have a line of code that interprets this value:
<?php if ($includeSidebar == 'yes') include 'sidebar.php'; ?>
This code will either include or not include the contents of a file called "sidebar.php." Notice that there is a double equals sign. The single equals sign is used to set a value. The double equals sign is used when interpreting a value. Also notice that there are parentheses around the condition. The parentheses are required.
Another useful application for PHP conditional statements is in assigning classes to elements for styling purposes. If you have a style called "selected" that makes a navigation menu item look selected, you would want to apply that style only to the menu item that was truly selected. You wouldn't want to apply it to all of the menu items at the same time. One way to do this is to create a variable called $selected and then create some PHP code to either assign or not assign the "selected" style to a given menu item. The web page would have this in the code:
$selected = 'home';
or like this:
$selected = 'resume';
or like this:
$selected = 'coursework';
and so on for each menu item.
In the template, you could have a section of code for each menu item that looks something like this:
<?php if ($selected == 'home') echo ' class="selected"'; ?>
This will put the word "selected" in the XHTML code for that menu item if the variable is set to "home." In the context of the list item of a navigation menu, the above code could look something like this:
<li<?php if ($selected == 'home') echo ' class="selected"'; ?>"><a href="index.php">Home</a></li>
The final code after the PHP is processed would look like this:
<li class="selected"><a href="index.php">Home</a></li>
You can make things a bit more complex than this, for example by assigning more than one class to a list item, as is the case in the Abigail sample web site. In that navigation menu, the link to the home page in the template looks more like this:
<li class="<?php if ($selected == 'home') echo ' selected'; ?>"><a href="/sample_website/abigail/">Home</a></li>
The end result looks like this:
<li class="selected"><a href="index.php">Home</a></li>
The "first" class can be used to style the first list item in the navigation menu differently than the other list items. You wouldn't have to do that, but you could. So in this case the styles for both "first" and "selected" are being applied.
PHP conditional statements can be used to do all kinds of things. For further reading, refer to the "if" entry in the PHP documentation (read also the documentation for "else" and "elseif").
A PHP Template System | 

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.