Nick Manning Nick Manning - 1 month ago 6
PHP Question

Can I use file_get_contents to load css?

Instead of this:

<head>
<link rel="stylesheet" type="text/css" href="css_file.css" />
</head>


I want to do this:

<head>
<style><?php file_get_contents("css_file.css");?></style>
</head>


For some reason though, the text shows up in the doc but the style is not applied. This is a two part question:


  1. Why is this not working?

  2. Is this a bad idea to do this?


Answer

This question was posted when I was a beginning developer. I didn't know about Sass and other easier and better ways to achieve variables in CSS. I also didn't know about browser caching.

Jeremy Karlsson answered the question in a comment. file_get_contents requires echo.

Comments