Simple file structure php question

I decided to use php includes for my static html website. The footer.php includes the javascript, but I don’t need certain scripts to load unnecessarily on different pages, obviously. It’s been awhile but, what are there any ways to avoid this? Thanks

Certainly. PHP works with conditions, so for example you could have something like:-

if($scriptA) {
     include_once "js/scriptA.js"
if($scriptB) {
     include_once "js/scriptB.js" ;

Or maybe if there are quite a lot of possible scripts, create an array of those included in particualr page.

foreach($javascripts as $script){
    include_once $script ;

There are probably many other ways, as always…

There are a variety of ways you could do this. For example, in the main php pages:

$useJQuery = true;
include "page-footer.php";

and in page-footer.php:

if ($useJQuery)
		echo "<script src='/site/js/jquery-1.11.1.min.js'></script>";

or if you had a bunch of optional included files, you could pass a value that was a bitmask of which files needing inclusion.

From memory include files can access any parent variables and the included files can access CONSTs, also the CONSTs can use arrays.

The included file needs a series of ifs:

if( defined["PARENT['val-001']")  ) : 

elseif( defined("PARENT['val/002']") ) :
  include 'js/Val/002.js';

  include 'js/default.js' ;


Tapping on a tablet and cannot test.


Whoops - Just searched and discovered variables can be included so amended the first incorrect statement.

I would rethink the decision to use php includes for delivering js files. You can but using html script elements would make more sense. Unless you are doing things like emails where the script elements might not work.

The thing about script elements is that there is a great deal of caching done automatically for you by the browser. So you can have multiple script elements but once the browser has loaded them once they won’t be reloaded. So it does not really matter if a particular page does not use everything.

1 Like

Maybe I am looking at this the wrong way. The idea of using includes is great for having core functions that are used on multiple pages but you only have to maintain one file. So why include things and then disable them ? Maybe move all or some js outside the footer.php, put different js in different includes and include footer.php and required js. I guess it depends how many different js scripts and how many permutations there may be of which ones load and which don’t. Is the decision made manually, by you or are you looking to have an ‘intelligent’ page that loads the js it needs - if so, how does it know.
If you need some program or page control over what is included -
The page that calls the include must be a .php page obviously, so you already have access to a very powerful decision making resource. Use multiple includes and php ‘if’ to decide which ones are included. I mean you can have a decision making php script, include that on every page. Give each page a php id, basically set a php var say,
Then the page can decide itself which php files to include based on the page type, there are so many things you can do if you have access to php.