Forum Moderators: coopster

Message Too Old, No Replies

Too many PHP includes? Possible.

         

andmunn

1:21 am on Apr 19, 2006 (gmt 0)

10+ Year Member



Hello,

I'm currently developing a website, and using PHP includes for things like the header, footer, and sidebar. However, i'm also using them for ad / google placement (the ad is an include, so i can later change it easy)...

All together, i have about 7 include statements in the page - it seems to work fine, but could this cause a problem?

THanks,
Andrew.

Don_Hoagie

2:54 am on Apr 19, 2006 (gmt 0)

10+ Year Member



nope.

Habtom

3:43 am on Apr 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It doesn't. But you might need to explore the functions include_once, require_once as opposed to just include and require.

Habtom

andmunn

3:53 pm on Apr 19, 2006 (gmt 0)

10+ Year Member



i'm confused what you mean "include once", "require" once, right now i use <?php include="file.html"> and thats about it -is this good?

Andrew.

Habtom

3:59 am on Apr 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[php.net...]
[php.net...]

Asia_Expat

4:26 am on Apr 20, 2006 (gmt 0)

10+ Year Member



I find this confusing.

Say for arguments sake, that you have 20 different html files and you add them to your pages with 20 separate 'require' tags (not require-once)... is that acceptable?

vincevincevince

5:08 am on Apr 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can just include() them all. In terms of your 20 different include files - try making one include file which includes all the other includes! That will save you time and it will be easier to maintain as you can then easily add a new include to every page.

The reason you should know about include_once() is that (for example) you may have one include file which connects to a database (connect.inc.php)

Your left-hand menu may be database driven - so that include (leftmenu.inc.php) will start with <?php include('connect.inc.php');?>

The main page itself (page.php) may also need the database, so you might, again, state <?php include('connect.inc.php');?>

In total you might end up connecting to the database twice during one page execution - that just slows things down and ties up resources.

Even worse is if you try including a file which contains a function definition twice. The second time you include it, php will realise that there is already a function of that name, throw an error, and die (even though the original function was the same file but included already).

A neat way to solve all those problems is by using include_once(). This does exactly what it says on the tin. It includes the file once per page-view / server request. If it sees <?php include('connect.inc.php');?> more than once, it only includes it the first time.