Forum Moderators: coopster

Message Too Old, No Replies

Efficient way of including code

Whats the mos efficient way to call an include?

         

madmatt69

4:18 pm on Oct 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I've got a few lines of php code I call at the beginning of all the pages on my website (with the exception of files in two sub-directories) and I'm trying to think of the most efficient way to include that code.

Right now I have the lines at the top of each file. So if I have to change something, I need to edit each file. Obviously a pain that could be fixed by calling the code in an include.

I'm just thinking of what the most efficient way of doing this would be? For example, so that it would maybe cache the include or reduce a call to the server by putting the code somewhere else, anything like that.

Any suggestions?

jatar_k

4:43 pm on Oct 29, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could include it on every page using require or include

you could look at auto_prepend [php.net]

PHP_Chimp

4:45 pm on Oct 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although I suspect that this isnt a very good solution you could have a single file that includes all of the include files.

example -
if you want to include a.inc, b.inc and c.inc on all pages
file - include.php
<?php
include 'a.inc';
include 'b.inc';
include 'c.inc';
?>

Then if you need to edit the includes just change them here.

Habtom

5:30 pm on Oct 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So if I have to change something, I need to edit each file.

I couldn't understand this part. If you have included your files once, how many times would you need to rename the files and change those lines to include the right file name.

There seems to be a sign that you might be getting into one of the Top 100 Signs That You are Writing Spaghetti Code in PHP [webmasterworld.com]