Why just use PHP variables for dynamic style sheets?

I read about dynamic style sheets and came across several options, including sass, and less. But my question is why not just turn my stylesheet.css into stylesheet.css.php and just use php variables. Then I avoid all the dependency issues associated with all of these other approaches.

Can I ignore some serious issues doing this this way?

+4
source share
3 answers

There is an argument for reusing code: when writing PHP code to generate CSS, you actually duplicate (some) the logic of things like sass and less. Why would you do this when there is a widely used, proven and complete alternative?

Another thing is performance. Standard CSS files are served by your web server using reasonable headers regarding browser caching. Your browser will not load the same CSS file every time, it just gets it from the browser side buffer. By default, PHP is not cached at all (and you would usually not want this). This means that by default your PHP-generated CSS will not be cached, incurring additional load on your server and additional waiting time for your client. Although some of them can be resolved (including the header title in the PHP code that your CSS generates), some of them cannot (for example, the overhead of starting a PHP web server).

+5
source

Can I ignore some serious issues doing this this way?

I place all the static assets on the CDN that you need too. CDNs do not do PHP.

Also: caching, runtime performance, minimization

+2
source

PHP variables used in inline CSS

Using PHP variables in CSS has many advantages, one of which is that you do not need to learn new syntax. Using PHP variables in CSS code is a well-known practice that has already been implemented in many frameworks, themes, and other scripts related to the site.

The most common use is inline CSS . The following is an example of inline CSS using PHP variables:

 <html> <head> <style> .class { color: <?php echo $text_color; ?> } </style> </head> <body> </body> </html> 

This method is commonly used when the PHP variable represents a user parameter set through the admin interface. One practical example would be in the WordPress Theme, where the user can set the background or text color through a theme backend.

PHP variables in an external CSS file

When it comes to external CSS files, it is also possible to use PHP variables, but to avoid a PHP file parsing your CSS file every time it is removed, you need to save the output to a static file, such as stylesheet-processed.css .

Both SASS and LESS need to be analyzed before saving in the " .css " file. The same goes for your PHP file, which you must execute and save the output in a static .css file, as well as in other syntax.

CSS file parsing is a very common practice and is widely used on many websites and on the most famous websites. Usually it increases site performance by decreasing (~ 25% saving) CSS code, merging multiple files into one (fewer HTTP requests), and gzip (saving ~ 80%) of the resulting files.

Here is an example of how you will use PHP variables in a file called stylesheet.php and save the result in stylesheet.css :

 <?php // Get the parsed CSS code with the $processed_CSS = file_get_contents('http://www.example.com/stylesheet.php') // Save the processed CSS to a static CSS file file_put_contents('stylesheet.css', $processed_CSS); 

Place the above PHP code in a file called " parse-css.php " and access it through your web browser to create or update the resulting static CSS file.

And then in your HTML code you should include stylesheet.css instead of stylesheet.php .

You can improve your parser so that it also reduces CSS code, for example, using the CSSMin PHP class.

+2
source

Source: https://habr.com/ru/post/1497672/


All Articles