Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Load css via php

Posted on 2016-07-14
18
Medium Priority
?
46 Views
Last Modified: 2016-07-15
Hi,

Is it possible to load CSS via php and if so, how could you do it? So, instead of :

 <link href="css/theme.css" rel="stylesheet" type="text/css" media="all" />

Open in new window


you use php to load this css file?
0
Comment
Question by:tofat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
  • +1
18 Comments
 
LVL 7

Expert Comment

by:rgranlund
ID: 41711335
<?php include('css/theme.css'); ?>
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41711336
PHP is a server-side technology.  PHP and MySQL are used to generate whole-cloth web pages consisting of HTML, JavaScript and CSS.  So the answer is "yes."  You can inject the CSS inline into the HTML document.
https://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html
0
 
LVL 7

Expert Comment

by:rgranlund
ID: 41711341
You can start there but there is a little more you should do.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41711351
Sorry, I forgot to highlight the part of the article.  See: The Timeline of Request and Response
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41711364
You can load your CSS with PHP but what is the point?  What are you trying to accomplish?
1
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41711599
I kinda share Dave's question here, too.  It seems like there is a backstory to questions like this, and when we can uncover the "deep down" roots of the question, there is often a simple (or simpler) design pattern that emerges.
0
 

Author Comment

by:tofat
ID: 41712888
Well, I am not having much luck using defer/async plus a bunch of other techniques to load js and css files without getting the below the fold errors from google insights. Unfortunately I tend to obsess about things once I really get an idea in my head, and my idea is that I want to get 100% or as close to as possible.

Some of the methods work somewhat but don't always work across browsers. I have a few posts on here already with various other methods that I have been trying but I read someone say somewhere on all my searches that you could use php to do it (I think it was a wordpress function actually, but I am not using wordpress).

I am not sure if using php would in fact work to prevent that dreaded google speed insights message or not and if it would work across browsers without breaking my layout.
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 41713174
PHP runs on the server and will deliver the files in the place you have the link in the page.  It will not defer it to later loading.  For what it's worth, I never do the things you're talking about to defer loading except for putting tracking scripts at the bottom of the page.  Everything else goes where it is needed which is often in the <head> section.
0
 

Author Comment

by:tofat
ID: 41713198
Yeah, everything behaves better when putting css in the head and Js before the closing body tag, but then I get that annoying above the fold error. I guess I should just stop obsessing over it and do it like that. But there is one particular website I found that has 100% and they have lots of css files and js. Am I allowed to post that page's code somewhere for you to look at? I would just like to know what it is exactly they are doing to get that. I don't know what is going on in there but since most of you here know 1000 times more than me, I am sure you would know. I will give you 500 points if you do, hehe  :)
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41713210
If you are trying to follow the 'recommendations' of Google Pagespeed, you should first go look at the 'View Source' of a Google search results page.  The largest thing in the <head> section is a couple of hundred lines of JavaScript.  And they put <style> sections throughout the body code.  Google is not following their own 'recommendations'.
0
 

Author Comment

by:tofat
ID: 41713216
Where else can I do these kind of tests if Google isn't the best?

And, you didn't say if you were prepared to look at that code?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41713259
Just post a link to the site and I'll look at it.  I don't want you to send me the code.

The point is that all these recommendations are coming from Google and they are not following them themselves.  You need to do what makes sense and works for you and then move on.
0
 

Author Comment

by:tofat
ID: 41713278
https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fwww.websitedesignpe.co.za&tab=desktop

http://www.websitedesignpe.co.za/

If you could explain some of the techniques it would be appreciated. Perhaps you could even tell me if they are good or bad in your opinion.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41713325
I'm sorry but that is almost fraudulent.  The page loads very slow the first time because it has at least 1000 lines, yes, one thousand lines of JavaScript in the <head> section.  About 80% of the page.  And that does not include the jQuery files that are being loaded.  Even when I load that page a second time, it takes forever for that top section to display properly.  It may 'test well' but I would not say that it 'works well'.
0
 

Author Comment

by:tofat
ID: 41713331
Yeah, I did notice that it had LOADS of code in the page which gave me a headache. So, do you think the site gets 100% because most of the JS is inlined?

And last question (promise), what does that bit at the bottom do with all the css files? Google doesn't pick them up but I am assuming it is because they are at the bottom of the page below a 1000 odd lines of code. What does that function do? (like I said, that is my last question, I just want to know what that function at the bottom does with the css files)
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41713343
That site is 'optimized' for testing, not functioning.  And as part of that... the javascript at the bottom loads some links to the CSS files so they won't be seen in the <head> section.  I didn't notice at first but something like 80% of the <head> section is the CSS <style> section.  And that's before the JavaScript loads more.
0
 

Author Closing Comment

by:tofat
ID: 41713363
Thanks for your patience and answering all my questions :)
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41713367
You're welcome, glad to help.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question