Re: PHP includes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Gary wrote:
Thank you to everybody that replied...but it almost seems it is making extra work.

I can understand using an include for a menu, since they tend to change often and it is on every page, but the normal content I am not understanding the benefit. If I have a page that has unique content on it, that is to say no other page has this content, why would I want to create a separate file to be included on the page, why would I not simple put the content directly on the page itself?

What is the best type of file to be used as an include (.txt, .php).

Thanks again for all your help.

Gary
""Gary"" <gwpaul@xxxxxxx> wrote in message news:8A.64.51087.33BF3B94@xxxxxxxxxxxxxxx
I'm working on learning php and have been toying with includes, and I am trying to figure the advantages/disadvantages to using them.

I know that using them eliminates the need to "put" the files once altered as with a template, however, is that the only advantage.

My particular concerns are with SEO and if the search engines and the bots can read the page if it is made completely on includes?

Any and all comments would be appreciated.

Gary




Hi Gary,

I think this thread is at risk of getting confusing and not addressing the problem at its root.

An include is typically used to allow storing reusable code in a file all by itself, this could be a settings (config) file, a class, an html template or whatever - purpose being of course to keep you all nice and organised, with each thing in its place.

Leaving out everything else and focusing only on why we'd use include/require in an application, let's take a simple example:

Simple PHP Site with 3 Pages.
1 - / (the homepage)
2 - /?page=about (the about us page)
3 - /?page=contact (the contact us page)

in the above scenario then your index.php could look like this

<?php

//set the default page to be home
$page = 'home';

//check if a page has been specified
if( isset($_GET['page']) ) {
  $page = $_GET['page'];
}

if( $page == 'home' )
{
  include 'home.php';
}
elseif ( $page == 'about' )
{
  include 'about.php';
}
else( $page == 'contact' )
{
  include 'contact.php';
}
else
{
  include 'page-not-found.php';
}

?>

very simple, but works; now let's expand it a bit.. in a real world scenario we probably have a good chunk of script at the top of index.php that holds database settings, database connection etc. and in each of our "pages" we probably have much of the html repeated (site header, footer, navigation) - we can then abstract these in to there own files to keep it all nice and clean.. as such

<?php
$page = 'home';
if( isset($_GET['page']) ) {
  $page = $_GET['page'];
}

// get site & database settings
require 'config.php';

// get our database class
require 'classes/class.database.php';

// start out database
$database = new DatabaseClass($settings_from_config);

// load the site header
include 'templates/header.php';

// load the content for the requested page
if( $page == 'home' )
{
  include 'content/home.php';
}
elseif ( $page == 'about' )
{
  include 'content/about.php';
}
else( $page == 'contact' )
{
  include 'content/contact.php';
}
else
{
  include 'content/page-not-found.php';
}

// load the site footer
include 'templates/footer.php';

?>

now everything is in its rightful place and we have lots of easy files to work with, for example now you can let a designer work on the about.php page knowing that they won't be able to break the whole application, as it's just a bit of html content.

also, you can now change the header image in one file (templates/header.php) and the whole site will update.

most PHP websites are simply far more complex versions of the above, look complicated but the principals are the same.

now the only thing I didn't cover is which to use.. it's often the case that require_once and include_once should be used over include and require; because in most cases (certainly with classes) you just want to ensure the code is available. both of the _once statements will only load the file if it hasn't been already, thus ensuring no errors such as "class already defined" because it's already been included!

On the other hand if the code in the file has been designed to be used many times on a "page" then include would be the better choice - a really stupid example but consider a file containing a fancy horizontal bar; you may want to include this many times on a page.

Finally, require and require_once will kill the script with an error if the file isn't found, whereas include will throw an error and simply keep on going if the files isn't found.

Think that covers everything in the scope of this :)

Regards!

Nathan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux