Forum Moderators: coopster
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.
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.