The difference is that tools like Less/Sass provide all these features of generating a dynamic CSS very close to CSS vanilla. The syntax of a Less file can easily be written and understood by a CSS developer, as it is not much different than what it is used to. Already doing this with PHP would require knowing a new language.
LESS file taken from http://lesscss.org:
@base: #f938ab;
.box-shadow(@style, @c) when (iscolor(@c)) {
-webkit-box-shadow: @style @c;
box-shadow: @style @c;
}
.box-shadow(@style, @alpha: 50%) when (isnumber(@alpha)) {
.box-shadow(@style, rgba(0, 0, 0, @alpha));
}
.box {
color: saturate(@base, 5%);
border-color: lighten(@base, 30%);
div { .box-shadow(0 0 5px, 30%) }
}
CSS file result from above code:
.box {
color: #fe33ac;
border-color: #fdcdea;
}
.box div {
-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}
Note that the syntax is very close. Even, many tools implement a syntax when they know that it will be officially implemented in CSS in the future. For example, I’ve seen tools they used mixins of the system of grid exactly with the syntax of grid present today in CSS. This allows the developer to migrate to the most modern CSS without changes in its code.
Already, providing the CSS code via PHP does not present any serious problems. Sending the HTTP response headers correctly and formatting the CSS correctly will be no problem. One difference is that while tools like Less/Sass generate a static CSS file, PHP will generate the file at each request; this can affect the response time of the request depending directly on the execution time of the PHP code. One of the advantages of using PHP to manage this is the ease of controlling all the headers of the HTTP response, especially those related to the cache.
Below is a compiled list of the differences cited. The sign on the side indicates which tool has the best results for each feature according to my opinion:
Code syntax;
- LESS/SASS approaches its syntax to the vanilla; ✓
- PHP has a completely different syntax;
Response time;
- LESS/SASS generate a static CSS file;
- PHP demands to be executed with each request, which can increase the response time;
Need for HTTP response headers;
- LESS/SASS headers are managed by the server itself;
- PHP demands that you manually control the headers;
Management of HTTP headers;
- LESS/SASS headers are managed by the server only;
- PHP enables you to manage all headers;
Another possible difference may also be in the ability to send the CSS via push server HTTP/2. I don’t know if forms would be possible in an equivalent way.