Echo contents of stylesheet

hi

i want to echo contents of external stylesheet between tags of my index.php

so which method is preferred

<style type="text/css">
echo file_get_contents('style.css');
</style>

or

<style type="text/css">
<?php include('style.css'); ?>
</style>

which is preferred method ??

thanks
vineet

I would prefer require(…) instead of include because if the style-sheet is missing then PHP errors will be activated which should show in your PHP error log file…

I think what is preferred would be

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

I think there are a couple of factors to consider.

If the style-sheet size is small and less than about 4Kb it would be advantageous to include/require the style sheet in order to minimise an additional HTTP request.

Another factor is the site’s bounce rate. If it is high then there is little advantage gained by using the link method for subsequent pages.

1 Like

its around 15kb in size.

so what do you suggest ??

vineet

also is there any speed different between these two functions for calling/ including content of css

file_get_contents
and
require_once

thanks
vineet

Hopefully that is the minimized size.

What is the web-page size.

Try some online speed tests such as PingDom and note the difference.

Try including the style-sheet for a week and notice any differences.

Unless the person is creating an AMP page which requires inline css :wink: Doing it this way would allow you to have a single stylesheet for easy editing but still create the necessary inline markup for AMP. (unless i’ve misread what is needed for AMP).

1 Like

Also specific AMP syntax:


<style amp-boilerplate> ... </style>

<style amp-custom> ... </style>

I find the new syntax infuriating, surely other means could have been detected and the original syntax retained?

what is AMP page ?? i never heard of it ??

vineet

Does that css file include php code? If it doesn’t than you can just include it the normal way with a link tag as shown by gandalf458.

hi

does anyone know why google is asking me to optimize a css file that is just 30kb.

i dont have any other css file on that website.

just 30kb ?? still google ask me to optimize it ?? why ??

vineet

Are we talking about Google’s PageSpeed Insights?

It is only a tool. It is telling you what you can save. You don’t have to do anything the site suggests. They are only suggestions. Some are helpful, others are nonsense. I had one page where it told me I could save 1 Byte!!

2 Likes

Hi,
its a new(ish) way of marking up a webpage to make it the more mobile friendly and fast It has certain differences to a standard webpage including making all of the css inline (ie you don’t call in any external css files). I forgot the syntax is slightly different which @John_Betong has corrected above. But it was just an example of where you might be better using php to insert css from another file.

sorry hopefully didn’t confuse things too much

1 Like

So for a 15k CSS file not using AMP that would definitely be the preferred alternative. (unless the styles.css file itself contains PHP in which case you’r rename the file as styles.php and have it serve the appropriate CSS MIME type header so that the file is recognised as CSS).

1 Like
  1. The speed difference is very small and you would need a very busy site to notice it.

  2. Do not use require_once - _once makes no sense in this case, it doesn’t really hurt but adds a microscopic overhead.

  3. The biggest speed difference will be if you use an opcode cache (opcache) on your server (which is always a good idea!). require and include will be picked and compiled by the cache so in practice the external file will not really be loaded after the php code has been cached in memory. file_get_contents, on the other hand, will have to be executed every time.

  4. I would still prefer linking in html like @Gandalf suggested.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.