Jump to content
xisto Community
chilipie

Tutorial: PHP Includes

Recommended Posts

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 :) . 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.

<?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

Share this post


Link to post
Share on other sites

There various type of include function.Some script do not work with include function but it may work will require function.PHP Functions:-includeinclude_oncerequirerequire_onceAll the above functions respond differently.Need more details?Let me know.

Share this post


Link to post
Share on other sites

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

<{POST_SNAPBACK}>


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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Some function only work with require and not with the rest.

I have personally experience that serveral times before with MySQL Query.

<{POST_SNAPBACK}>


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.

 

 

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

<{POST_SNAPBACK}>


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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Also- if you do not put the <?php ?> tags in, the browser will count it as HTML. so if you put this in an include file: $test1 = test2 echo ("$test1")the browser will count it as HTML. But if you put in the PHP tags it will display the echo script

Share this post


Link to post
Share on other sites

You're right. I would much rather use PHP or some other include method, but I couldn't get those to work. and as for JavaScript not being enabled, my pages will still work even with out the includes. The pages just won't look quite as good, but will still be completely functional. But you are right: I wouldn't recommend JS includes either.

Share this post


Link to post
Share on other sites

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.sedoparking.com/shothost.com/order

With my links directory I did the same thing:
http://www.sedoparking.com/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 :)

Share this post


Link to post
Share on other sites

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.....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.