PHP Includes
How do I use PHP Includes?
PHP Includes is a function of PHP that makes web development simpler than ever. Let's say you have a website, and it has 50 pages. On those pages, you've defined link colors, text colors, image placement, etc. Now, what if you want to change your layout? Normally, you're stuck having to change the properties of all 50 pages. With PHP Includes, you'd only have to change 1.
Or, let's say you have a group of images you want to add into specific places on your page, but the code gets long, messy and annoying, or maybe you'd like to change the images themselves once in a while, without having to find each page and snippet of code to change one image. For this, you could use PHP Includes. By inputting one line where you want the images to show up, and having it link to an outside page holding your actual code, you'd only have to change the page with the code itself.
Step One
The first thing to do is create your outside page. Open Notepad, or a similar text editor. On this page is where you'll put the actual code that you want to 'call in' to your other pages. For instance, if I was going to keep my header, title information, and maybe background, text and link properties on a PHP page, and call it into the other pages, I would create a notepad document like this:
See, this is what usual website headers look like ... approximately. Instead of having to have that information on every single page of my website, and having to change each and every individual page for one minor color change, I can use PHP to make it much simpler to use, and to modify.
Save the text document that you created in Notepad, and name it something relevant. For instance, the one I'm using here, I would save it as 'header.txt', like below. Notice how the title of the notepad document has changed.
Then change the file extension from txt to php and upload it to your server.
Step Two
Now we have to use PHP to call the file into your page. Let's say you're working on your main page, and for the sake of this example, we'll have a page titled main.html.
On your page, at the very top where the header information would normally go, we add this piece of code.
<?php include '/home4/htsxpjte/public_html/header.php';?>
Make sure that you have the URL pointed to the right place on your server, where you've uploaded the PHP file.
Step 3
Now you have to change the file extension of your main page, the one we called 'main.html'. In order for the PHP script to be displayed, your file extension has to be php compatible. You can change your file extension to either .php or .phtml. (I would recommend .phtml, so you don't get your actual pages confused with your scripts!)
When you've done all this, you'll see that the information has been called into the page, in the exact place you used the PHP includes file.
There is no limit on the amount of these that can be used per page, and they will not conflict with each other if they're all individually named. This script can prove invaluable to any designer.