Use or not use CSS/JS within PHP?

Asked

Viewed 683 times

5

Using CSS/JS inside PHP, is there a difference if used in an external file for example? Many pages have CSS/JS in and also externally, it would not be easier to put all together than to split one part in the code and another external on the same server?

What is the right way to use them? And why?

  • "inside PHP" would be where exactly? Putting all the code inside the file .php or using the files on the same server?

  • Putting all the code inside php, no external files. @Kazzkiq

  • @Kazzkiq Your answer was good, if you want to post with small adaptations on question that Sergio has indicated, functionary.

  • 2

    @bfavaretto as it is, I reread the question here and I thought my answer got a little out of context, luckily I saved it in a notepad before deleting it, it was used in another question.

  • 2

    I would venture to answer in a more extensive way if I had a little more time, but I dare say now: There is no right or wrong. All situations have advantages and disadvantages, the best is to know what they are, and see what applies best in the real case. A simplified example: External CSS may be useful for caching the browser, but in certain projects, its separate and/or delayed HTML load is unacceptable aesthetically. In other cases, a giant CSS inside every page is not good in terms of speed and bandwidth. It goes from the concrete case, regardless of the feature.

2 answers

7


It’s all a matter of rapidity and resource saving.

Putting all the code inside the page is not feasible, as repetitions of scripts and style sheets on all pages generate a very large and unnecessary stream of data.

To give you a sense, let’s assume that your CSS/JS has a total of 100kb. If you put all the code inside the page, a thousand hits will generate a total of 98mb transferred, a waste of bytes.

When placing CSS and JS in external files, they are downloaded only on first access.

This was one of the main arguments of the tableless movement, advocating that the use of CSS would also bring faster and resource savings beyond aesthetic gain.

As a general rule, make your PHP generate code that is only used on that page and/or that depends on user preferences. If your system allows the user to change the color of an element, it is no problem to generate the CSS within the page.

Conversely, overstating the dose and spreading your code across many different script files will increase the number of GET requests, which in addition to slowing down the site, also costs more (services like Amazon S3 charge for the number of Gets).

So the ideal is:

  • leave your scripts and style sheets out of HTML;
  • combine JS and CSS files before moving from development to production.
  • 2

    It is a question of speed and economy of resources, but also a question of readability. Mixing PHP, CSS, Javascript all in one file can make your application very unreadable. For a long time there was a lot of emphasis on moving CSS and JS to other files because often the code got confused. If your application starts to become unreadable with a lot of CSS and JS inline, it might be the case to think about putting them in separate files and putting the files together in one while running (as well as using a minifier). Now, if the inline code doesn’t impair readability, there’s no problem.

1

Hello friend.

In addition to the issues cited by @Ifarroco, using CSS and JS scripts in external files also helps create cleaner, more standardized code. Let’s say your CSS will be used on multiple pages. Creating it in a separate file, you can use it where you need it, just by inserting the file link. The same goes for JS.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.