iWillGetBetter iWillGetBetter - 1 year ago 106
PHP Question

All PHP pages include the entire set of CSS and JS, efficient?

I'll summarise. Please correct me wherever I was not able to phrase my question correctly.

I have a few PHP pages, all of them have the following format:

include "header.php";
include "footer.php" ?>


<!DOCTYPE html>
<meta charset="utf-8">
<!-- Main CSS -->
<link href="css/style.css" rel="stylesheet" type="text/css">


<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<!-- Theme JavaScript -->
<script src="js/main.js"></script>


I am new to PHP and not sure if this is the correct way to efficiently structure my PHP files because my concerns are:

  1. Each PHP page now loads the same navigation menu and footer. This is intended and is ok. However, it also loads ALL CSS and JS together, even when there are lots of lines of CSS and JS where it is not actually useful in that specific page BUT used in other pages. Is this a need for concern and if yes what ways should we go about doing this?

  2. Should I separate my main.js, style.css, header.php and footer.php so that each PHP page loads the minimum amount needed for the body functions?

  3. What is the standard practice when dealing with this case?

Would appreciate it if you can give some advice!

Answer Source

If you want to reduce the ammount of css/js on your page, then you can do something like this... Call your CSS with:
<link rel='stylesheet' type='text/css' href='css/style.php' />

Inside style.php it would look like something like this:

    case 'index.php':
      echo 'CSS code for index.php gos here';

    case 'login.php':
      echo 'CSS code for login.php gos here';  

Unless you've got like lots of styling and javascript which is confirmed to be seriously increasing load time, then it's fine and I wouldn't do the above.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download