All PHP pages include all CSS and JS, efficient?

I will summarize. Please correct me, wherever I can formulate my question correctly.

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

<?php
include "header.php";
?>
INSERT PAGE SPECIFIC MATERIAL HERE
<?php
include "footer.php" ?>

header.php

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

footer.php

    <footer></footer>
    <!-- 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>

</body>
</html>

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

  • Each PHP page now loads the same navigation menu and footer. It is provided and suitable . However, it also loads ALL CSS and JS together, even if there are many lines of CSS and JS, where it is not really useful on this particular page, but is used on other pages . This is necessary for concern, and if so, in what ways should we do this?
  • main.js, style.css, header.php footer.php, PHP , ?
  • ?

, - !

+4
4

main.js, style.css, header.php footer.php, PHP , ?

CSS JS -. php , css, :

  1. , css js. . css js php, php . , , , -, .

  2. css js php, js css. ? , 10 js 10 css head! SEO.

  3. CSS JS. , 100- CSS js SEO.

  4. CSS . . , , @media print{} , .

  5. css js . css js, css js .

, css js , .

+2

css/js , - ... CSS : <link rel='stylesheet' type='text/css' href='css/style.php' />

style.php :

 <?php
 switch(basename($_SERVER['PHP_SELF'])){
    case 'index.php':
      echo 'CSS code for index.php gos here';
    break;

    case 'login.php':
      echo 'CSS code for login.php gos here';  
    break;
 }
?>      

javascript, , , , .

+2
<?php 
$filename = basename(__FILE__, '.php'); 
?>
<link rel="stylesheet" href="css/<?= $filename ?>.css" />

...

<script src="js/<?= $filename ?>.js"></script>

: 1. CSS JS PHP. 2. PHP CSS JS

PS . Minimizing all your styles and scripts with just one file loads your pages faster.

+1
source

Yes, this is a good approach to code management. To include the same header and footer, you can easily add / update / delete menus and other functions without editing each file.

0
source

Source: https://habr.com/ru/post/1656784/


All Articles