php function for gathering includes...is there a better way than this? I am building a dynamic site. All data will be served through an index.php in my webroot, using jquery to update the content. In webroot, I have a folder 'i/' which contains the files I want to include. The main include files contain code which will be used on every page: globals.php, globals.css, globals.js, etc. plus library files, such as jquery.js. Then there are secondary includes that are used depending on the section or topic that has been loaded: topic1.php, topic1.css, topic1.js, etc.

I have written the following php function to call all the files that need to be declared in the head (stylesheets and javascript for now, but wanted to make it easy to add more filetypes in the future), allowing globals.php to be included before the html tag (and will probably include topics.php from inside globals.php). However, I'm not really sure if this is a very efficient function, or if there is a better way to do what I'm trying to:

function getIncludes($topic){
    $type=array("css","js");
    $library=array("js"=&gt;array("jquery"));
    $scope=array("globals",$topic);
    foreach($type as $ext){
        $includes=array();
        if(array_key_exists($ext, $library)){
            foreach ($library[$ext] as $file){
                array_push($includes, $file);
            }
        }
        foreach($scope as $file){
            array_push($includes, $file);
        }
        foreach($includes as $file){
            if(file_exists('i/'.$file.'.'.$ext)){
                if($ext=="css"){
                    echo '&lt;link rel="stylesheet" type="text/css" href="i/'.$file.'.'.$ext.'" /&gt;';
                }
                if($ext=="js"){
                    echo '&lt;script type="text/javascript" src="i/'.$file.'.'.$ext.'"&gt;&lt;/script&gt;';
                }
            }
        }
    }
}


Is this a good way of achieving my goal, or can I do better?
Don't make your directory structure so vague. Coming into this with no knowledge of your system how am I supposed to know what the "i" directory is? Just call it includes. Also, it is usually a good idea to separate your files based on their types so that they are easier to find. There are many common project directory structures out there. For instance I'm using something similar to Zend's. It looks similar to this.

/root
    /application
        /configs
        /models
        /views
        /controllers
    /data
        /cache
        /logs
    /public
        /js
        /css
        /images


I hope those file names don't indicate that you are actually using globals... There are much better ways to get "global" scope variables without ever using global. Sessions for instance. Globals, in my opinion, should eventually be deprecated. They are an old feature that has been proven faulty and replaced.

array_push() is another old function. Though there is nothing wrong with it, the preferred way to do it now is like so.

$includes[] = $file;


What you are trying to do is usually done with templates rather than PHP. For instance, you'd have a single HTML file with these includes in it already then dynamically change the contents with PHP. For those files that use the $topic variable, you would just perform a check to make sure it exists before including it via PHP. So a very basic template might look like this.

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script type="text/javascript" src="i/globals.js"&gt;&lt;/script&gt;
&lt;link rel="stylesheet" type="text/css" href="i/globals.css" /&gt;

&lt;!-- SPECIFIC INCLUDES --&gt;

&lt;?php if( is_file( "i/$topic.js" ) : ?&gt;
&lt;script type="text/javascript" src="i/&lt;?php echo $topic.js; ?&gt;"&gt;&lt;/script&gt;
&lt;?php endif; ?&gt;

&lt;?php if( is_file( "i/$topic.css" ) : ?&gt;
&lt;link rel="stylesheet" type="text/css" href="i/&lt;?php echo $topic.css; ?&gt;" /&gt;
&lt;?php endif; ?&gt;

&lt;!-- SPECIFIC INCLUDES --&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;?php if( is_file( "i/$topic.php" ) ) { include "i/$topic.php"; } ?&gt;
&lt;/body&gt;
&lt;/html&gt;