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.
"inside PHP" would be where exactly? Putting all the code inside the file
.php
or using the files on the same server?– Kazzkiq
Putting all the code inside php, no external files. @Kazzkiq
– Florida
@Kazzkiq Your answer was good, if you want to post with small adaptations on question that Sergio has indicated, functionary.
– bfavaretto
@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.
– Kazzkiq
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.
– Bacco