PHP Tutorial - Includes
Alright then, here goes my attempt to explain some php. This tutorial will show you how to use variables to do something useful - a way to include content in a certain place, without using iframes (that’s a good thing).
How?
Ok then, although I will paste the code at the end, I am trying to explain how this works - so here’s a quick run through.
Definitions
Variable - a ‘holder’ for data, which can be used throughout the code
URL variable passing - this is where you pass a variable (or variables) into another page, in the URL (address bar)
Include - a built in php function, whereby you can include a page in a similar fashion to iframes, uses the syntax include(’what-to-include’); - nice and simple eh?
IF…ELSE - this is a simple construct used in almost all programming languages, and does what you would expect - IF something is true, do something, ELSE do something else. This is useful, because you can make decisions in a page, say if the variable $name = bob then print on screen ‘Hello bob’. It takes a slightly more complicated syntax to include, as seen below.
IF (conditions) {
statements to be executed
}
ELSE {
statements to be executed
};
ARRAY - an array is basically a variable that can hold more than one value, arrays are arranged with an index (number) for each value, for example arrayname[1], arrayname[2], arrayname[3] etc. for each value in the array.
ISSET - A built in PHP function, which checks to see whether a variable is set or not, which i frequently use for validation, and to stop nasty warnings coming up… and its generally good programming practice to have something in place (even just a text message saying what has gone wrong) in case something goes wrong.
EXIT - Another built in PHP function, which stops code running after it, used here if the $page variable is not something you have allowed - this *should* not happen normally, so the code will only terminate if someone purposely changes the $page variable.
NOTE: if you don’t need an ELSE, then simply put a semi-colon - ; at the end of the third line, and delete the rest.
Making it useful!
Ok then, I hope you understood that, here’s how it can be used. Say you have a content area on your web site, and a decorative frame, or header/footer around it.. if you code it normally (all html) you would need to change e-v-e-r-y page, if you wanted to change a word in the frame - tedious at best.
This is where the php include comes in, you can have your frame in one file (index.php, say) and then in the content area (usually a table cell) you place the following code, and whalah! the content is included (hence the name) into the content area - and your frame only needs to be altered on one page.
The code.. and explanation!
| <?php $links = array(‘news.php’, ‘about.php’, ‘portfolio.php’); // define the array - links if (isset($_GET[‘page’])) { // if the $page is set if(in_array($page,$links)) { // if page is in the array include($page); // include $page } // end include page if in array statement else { // if $page is NOT in the array echo ‘bog off!‘; // diaplay a warning exit(); // end page running }; // end else statement } // end if page isnt set statement else { // if $page is NOT set include(’news.php‘); // include default page }; // end if $page is NOT set statement // *********** DISPLAY LINKS ************ echo ‘ <a href=$PHP_SELF?page=about.php>about</a><br> <a href=$PHP_SELF?page=portfolio.php>portfolio</a><br> <a href=$PHP_SELF?page=news.php>news</a> ‘; ?> |
In action
Here - [ link ] - is an example of includes at work - news and poll are includes, so they can be independently edited, and new links only have to be added once. Another advantage is that your code looks a lot cleaner, and content can expand dynamically, something that to the best of my knowledge, cannot be achieved with pure html.
Extend the code
If you want to add more links, simply add them to the array - put a comma , after the last entry, then the new filename in single quotes ”.
Thanks
Thanks go to TrigBoy, Recessive, Oxy and Jeb (and anyone I forgot) from tutorialforums.com - for pointing out some security holes in my original tutorial ![]()