Question

One of the things many web designers learn is that the more things you have that are external to a page, the longer it takes to fully load the page, because more server requests are made.

The most common way people style their pages is by using something along the lines of:

<link rel="stylesheet" href="style.css">

Which, of course, is a perfectly fine solution with no problems to it. However, it's a HTML-based call to an external file, which means the user requests an additional resource from the server.

Now correct me if I'm wrong here, but since PHP is server-side, if I were to use the include function to call an external file, the server would be the one to put the external file contents into the file it was called from, and then delivering the page to the user, without the user making additional requests.

If this is true, how feasible is this?

<head>
    <style>
        <?php include "style.css";?>
    </style>
</head>

While I'm aware that loading times for just one external call wouldn't make a world of difference, but as far as my line of thinking is concerned, is this correct, and is this an improvement over the common link rel method, even if by a small amount?

Regarding the include function, I'm aware that there are security issues, but let's just ignore those. If someone had access to your server to be able to exploit such a security risk, a little include function will be the least of your worries.

Was it helpful?

Solution

Commonly, you are going for this method:

<link rel="stylesheet" href="style.css">

because the browser is caching it.

You load the page once, and all external pathes are cached. When you reload the Page, you don't have to load anything again.

The 'include-method':

<head>
    <style>
        <?php include "style.css";?>
    </style>
</head>

loads the external stylesheet again and again, when you reload the page - nothing is cached!

Conclusion:

If you load the page just once, the include method is a little bit faster. If you load the page more than just once, the HTML-link method is alot faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top