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"=>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 '<link rel="stylesheet" type="text/css" href="i/'.$file.'.'.$ext.'" />';
}
if($ext=="js"){
echo '<script type="text/javascript" src="i/'.$file.'.'.$ext.'"></script>';
}
}
}
}
}
Is this a good way of achieving my goal, or can I do better?