PHP shortcuts for the non DEV ("includes")

php-Include-hero

Includes are really a nice way to clean up your HTML so that it is easy to work with. An include in PHP is something that can save you tons of heartache.

What is an INCLUDE?

Imagine you have to create a 5 page HTML document. In that html document you will most likely have a Navigation. You will have to create it the same on every page. Now.... Imagine your client calls and they need to add a new page. Now you have to go to all of those html documents and modify the navigation on every one.

OK - So far - Not a big deal...but when web sites get bigger... it is easy to miss stuff.

With a PHP include, Instead of  creating the navigation on each page... You create it once and CALL it on every page.

This is super simple.

 

Imagine you have some basic html

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>

<body>

<ul class="navigation">
<li><a href="">HOME</a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
</ul>

</body>
</html>

Instead of creating the navigation piece 5 times on each page...You can put just that piece of code out and save it in a folder on your web server called ...um...let's call it "includes"

So your file structure might look like this:
[images]
[js]
[css]
[includes]

or something like that.

Make sure you save the file with the extention PHP

So let's pretend we took just the code we want to replicate:

<ul class="navigation">
<li><a href="">HOME</a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
<li><a href=""></a></li>
</ul>

And we save it as "navInc.php"

So now our above code needs to call that file.

First we have to make sure it is saved as a php file. So if it was index.html it should now be saved as index.php

And in place of the code we cut out we are going to write a php include.

A php include is super easy to write and will save you so much time.

first I'll show you what the line of code looks like then I will tell you what it does.

<?php include ('include/navInc.php');?>

See!??! Super simple.

So this is what it looks like in the code:

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<?php include ('include/navInc.php');?>
</body>
</html>

The first thing we are saying is that we are going to include a PHP statement
<?php

Next - We say what we want to do
include

THEN- We say where to find the file we want to include. 
('include/navInc.php')

Of course - we have to execute the command - the semicolon tells the computer...we're done ...go get it..
;Lastly - We are done with PHP and going back to HTML - .
?>

Now go have fun. Next - I'll show you how to change the code based on what page you are on.
This will allow you to throw active classes on your navigation.