Tutorial: PHP Includes

Pages: 1, 2
free web hosting

Read Latest Entries..: (Post #17) by Coach on Apr 16 2005, 04:48 AM. (Line Breaks Removed)
QUOTE(chiiyo @ Apr 15 2005, 12:57 PM)Hmm... maybe use absolute URLs? Instead of using relative urls like "../directory/index.html" you could use "http://whatever.astahost.com/directory.index.html" in your embedded file (in this case, file.php) so that you won't have to worry about the script staying in the first file and not budging.Not a very elegant solution, but it ... read more.
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > PHP

Tutorial: PHP Includes

chilipie
This tutorial shows you how to put one HTML file inside another using PHP. This technique is very useful for menus, where you might want the whole menu to change sitewide - much easier to change 1 file than 50 smile.gif . I have also used this for footers and headers; you can use it anywhere you want to be the same on each page of your site.
CODE

<?php include("file"); ?>

Now I will tell you what it all means.
<?php and ?> - open and close the PHP statement (a bit like HTML tags).
NB. You may have seen the above written as <? and ?> - but this will only work if your server has short-tags enabled. To acheive maximum compatibility, use <?php and ?>.
include - tells the script to include a file.
("file"); - the location of the file you want to include (I normally give includes an extension of .inc or .php, but you can use .htm, .html etc.)

The page the include function is in must have an extension of .php.

And there you go! A page with a PHP include.

-----

I hope you enjoyed my tutorial, if you did, you might like the one on custom error pages with .htaccess

 

 

 


Reply

avalon
There various type of include function.
Some script do not work with include function but it may work will require function.

PHP Functions:-

include
include_once
require
require_once

All the above functions respond differently.

Need more details?
Let me know.

Reply

chilipie
I've never heard of the above functions before - I'm a bit of a newbie at PHP smile.gif . What exactly do they do and how do they differ from the include function?

Reply

mastercomputers
QUOTE(chilipie @ Oct 29 2004, 03:45 AM)
I've never heard of the above functions before - I'm a bit of a newbie at PHP smile.gif . What exactly do they do and how do they differ from the include function?
*



They do exactly the same thing except produce different errors.

require, means the file is actually required else if it's not there, you'll get an error and the script will stop.

include, means it'll try to include that file, but if it's not there, it'll produce a warning instead but the script will continue to process.

include_once, require_once exactly the same except for error/warning messages if file doesn't exist, as well as seeing if the file has already been used. It'll not include it again. Usually used inside functions, where you don't need to include that file again. e.g. you're including a configuration that sets variables and if you include it again, you would overwrite already defined variables which you don't want to happen.

Check the PHP Manual for more information on these functions and others.


Cheers,


MC

 

 

 


Reply

avalon
There is also difference in responding time.
Generally, include and include_once load fast than require and require_once.

Some function only work with require and not with the rest.
I have personally experience that serveral times before with MySQL Query.

Reply

r3d
QUOTE(avalon @ Oct 29 2004, 03:11 AM)
Some function only work with require and not with the rest.
I have personally experience that serveral times before with MySQL Query.
*



coz the speed of php i prefer using require_once plus with a nice error handler wink.gif

Reply

cargeek
okay, i am an extreme newbie to php....as in, i'm reading all i can so i can try and write my first script...so this maybe a completely rediculous question, but....if i'm wanting to use this php script to include an .html file as a menu...which one of these would i want to use? would include be sufficient or would it be better to use require or require_once?

stephen

"beware of programmers who carry screwdrivers."
--leonard brandwein

Reply

Hercco
QUOTE(avalon @ Oct 28 2004, 09:11 PM)
Some function only work with require and not with the rest.
I have personally experience that serveral times before with MySQL Query.
*



What do you exactly mean by this? There should be no difference for the script be your files included though include or require, as long as the files are there and are readable by the script.


QUOTE(cargeek @ Jan 20 2005, 04:38 AM)
okay, i am an extreme newbie to php....as in, i'm reading all i can so i can try and write my first script...so this maybe a completely rediculous question, but....if i'm wanting to use this php script to include an .html file as a menu...which one of these would i want to use? would include be sufficient or would it be better to use require or require_once?

stephen

"beware of programmers who carry screwdrivers."
--leonard brandwein
*



It depends. Require causes the script to terminate if the inclusion failes. Ie. if your menu files is missing or the script doesn't have permissions to read it. Include prints and error but continues the execution of the script.

So using include, your page will have an error message instead of your menu, with require there will be nothing else but error message.

If you page is useless without the menu, I mean completely, you can use require. But the situation is rarely such, so I would go with include.

Include is aimed for that kind of tasks exactly. Loading menus or content, which aren't absolite neccessity for your site.
Require is for loading functions or a settings file. It is quite pointless to continue execution of a page which gets stuff from database if database settings file cannot be loaded.

Reply

Brian
I use includes for all my pages, and I tried to use PHP, but could never to get it to work on my old server so I started using JavaScript includes. I learned all that from this page that has great info about PHP, JS, and other includes.

Reply

Hercco
Javascript includes are not a very good idea... As you probably know, if Javascript is turned off, isn't supported or otherwise doesn't work your includes won't be loaded. It also increases the page loadtime a bit, as the browser does another request when it parses the Javascript.

My opinion is that javascript includes, should only be used the way they were intended, ie. including only javascript code.

Reply

Latest Entries

Coach
QUOTE(chiiyo @ Apr 15 2005, 12:57 PM)
Hmm... maybe use absolute URLs? Instead of using relative urls like "../directory/index.html" you could use "http://whatever.astahost.com/directory.index.html" in your embedded file (in this case, file.php) so that you won't have to worry about the script staying in the first file and not budging.
Not a very elegant solution, but it might work if you only have a few files with this problem.
*


Ii works in something like:
QUOTE(Adapted from others websites)
<html>
  <head>
      <title>Include Sample</title>
  </head>
  <body>
    <?php
            include("header.php");
      ?>
      <table width="95%" cellspacing="15" cellpadding="5" border="0">
        <tr>
            <td valign="top" width="120">
              <?php
              include("menu.php")
              ?>
            </td>
            <td valign="top">
  <?php
switch ($_GET['c']) {
    case '1':
    include("content" . $_GET['c'] . ".php");
        break;
    case '2':
    include("../www/file2/content" . $_GET['c'] . ".php");
        break;
    case '3':
        include("../www/file3/content" . $_GET['c'] . ".php");
        break;
    default:
        include('content1.php');
  break;
}
?>
            </td></tr></table>

      <?php
            include("footer.php");
      ?>
  </body>
</html>


The $_GET['c'] comes from:
QUOTE
<p align="center" style="font:bold 1em sans-serif;color:#000099">
  <a href="index.php?c=1">Home</a><br>
  <a href="index.php?c=2">Reality</a><br>
  <a href="index.php?c=3">Mision</a><br>
</p>

Reply

chiiyo
Hmm... maybe use absolute URLs? Instead of using relative urls like "../directory/index.html" you could use "http://whatever.astahost.com/directory.index.html" in your embedded file (in this case, file.php) so that you won't have to worry about the script staying in the first file and not budging.

Not a very elegant solution, but it might work if you only have a few files with this problem.

Reply

finaldesign
I recently had some problem with includes. So here it is, if you know solution lemme know...
Problem:
If you have one php file in your public_html/ folder, called index.php, and in that index.php you include another file which is in public_html/directory/file.php - and in that included file "file.php" there is another inclusion of public_html/directory2/file2.php how can you manage to work it out... ?

It gets me lots of errors, and I end with solution that it can't be done, because if I include something in some file in root directory (e.q. index.php) script will stay executed in that directory (hipoteticly) and if I include file which has line of code in it self
include("../directory2/file2.php");
what it really does it goes directory back beyond public_html and finds nothing... - complicated huh sad.gif ? and I cannot change the code of that file.php because Im using it in another file and that file is in folder public_html/directory/ so that file needs that ".." part of code in include...
How to solve this complication? any suggestion for different programming...?

Reply

tlc
I have a question.. I had truble to put include text file without that it would but "1" in the end of the text? Where It came from? Finally I got rid of it, but still I don't understand why.....

Reply

prodigy
Yes includes are a good method of optimising a template. I enjoy using includes into certain CMS's and other various template driven scripts. All that needs to be done is to include the html page (or other) below the split code of your main html (or other) page. Eg have a header and a footer, with content inbetween (which is driven by the script). If you do this i suggest you install these scripts into seperate folders with your header aand footer html (or other) pages. Sort of hard to explain as it varies with certain scripts, but it is simple to implement.

Heres what I meant:

With my order system i did a little bit of templating: http://www.shothost.com/order

With my links directory I did the same thing:
http://www.shothost.com/links

Normally these scripts have a default boring look, but I have sort of customised it in a way, nothing to flash but it works for me.

Maybe you guys could experiment smile.gif

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Pages: 1, 2






*SIMILAR VIDEOS*
Searching Video's for php, includes
advertisement




Tutorial: PHP Includes



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE