PHP includes are an important part of PHP. With HTML sometimes it gets very annoying having to change everything if for example you change your layout, it can be very time consuming. PHP includes break down this process so that can have a header.php file and a footer.php file and these will appear on every main page, so if you were to change your layout you would only have to change these two files.
1. Here is an example of the HTML page, it is a very simple page to make it easier for you to understand. Basically this coding would be on every individual page but the only bit that would change would be where the content is. So you see how inefficient this is?
2. Take the page header
And save this as header.php
3. Take the footer and end html and body tags
and save it as footer.php
4. Now you will be left with the middle part... this should change with each page as this is the actual content. So now we can join this together
Continuing, you could always add an include in the middle that contains your page content (content is placed in content.php
This makes changing layouts simple, without having to change EVERY page every time.
5 . but say you wanted to have a page.php?id=whatever to include content, no problem. This example is more complex, but still very basic.
and thats all you need
Now if you want to do mod_rewrite... say your page was index.php?id=foo and you wanted to do like index/foo
and save that as .htaccess
Now if you wanted to go further with that, you'dhave to edit the above code to your liking but i hope i got you started.
more information MOD_REWRITE here
If you like my tutorial, please consider donating a few points to me
1. Here is an example of the HTML page, it is a very simple page to make it easier for you to understand. Basically this coding would be on every individual page but the only bit that would change would be where the content is. So you see how inefficient this is?
HTML:
<html>
<head>
<title>Your page title</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="content">Main content here</div>
<div id="footer">Copyright</div>
</body>
</html>
2. Take the page header
HTML:
<html>
<head>
<title>Your page title</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head><body>
3. Take the footer and end html and body tags
HTML:
<div id="footer">Copyright</div>
</body></html>
4. Now you will be left with the middle part... this should change with each page as this is the actual content. So now we can join this together
HTML:
<?php
// include header
include ("header.php");
?>
<div id="content">Main content here</div>
<?php
// include footer
include ("footer.php");
?>
PHP:
<?php
// include header
include ("header.php");
// include content
include ("content.php");
// include footer
include ("footer.php");
?>
5 . but say you wanted to have a page.php?id=whatever to include content, no problem. This example is more complex, but still very basic.
PHP:
<?php
include ("header.php"); // the header
if(isset($_GET['id'])){ // if ?id= is found
$id=$_GET['id']; // convert the $_GET to variable form
if(is_file($id . ".php")){ // if the file is a php file, include it :D
include $id . ".php"; // the actual including of the file
} else { // if the file isnt found
echo("404 -- file not found!"); // echo the 404 error message
}}} // close the tags
else include("home.php"); // if there is no ?id= found, include this page
include ("footer.php"); // the footer
?>
Now if you want to do mod_rewrite... say your page was index.php?id=foo and you wanted to do like index/foo
Code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule index/(.*)/$ /index.php?id=$1
and save that as .htaccess
Now if you wanted to go further with that, you'dhave to edit the above code to your liking but i hope i got you started.
more information MOD_REWRITE here
If you like my tutorial, please consider donating a few points to me
Last edited: