Can you import an external CSS file into PHP using the same code as HTML? Try it.
I know you can’t use the same code but it’s a lot similar.
I had Googled the solution many times but I couldn’t find the exact answer and kept trying. At the end, I found on a reference book so I am sharing here. I thought someone might need it.
If you try the following code the the PHP page is going to print the whole CSS code in some cases and in others, it won’t do anything at all.
php include 'my-blog-header.php'; include 'my-blog-main-style.css';
//add opening and closing tags code ?>
In above code, all you need to do is surround the .CSS file line with a style tag.
Look at the following code.
// USE THIS CODE TO ADD EXTERNAL CSS FILE INTO PHP
php include 'my-blog-header.php'; ?> //INCLUDING EXTERNAL PHP FILE
//add opening and closing tags code ?>
There is one advantage as well. In the above way you can use your PHP file into your CSS file which can be really useful to organize it for e.g. colors as variables.
NOTE: style tag does’t need to be echoed so the following code can be used for better programmer readability.
Also, all the HTML file system rules applied here as well. So you can use any file from any folder but you need to follow some rules.
file import