Of course it is possible. The simplest way would be to have your CSS in a PHP file that you would link directly as a normal style sheet. For example:
css.php
<?php
$azul = "blue";
?>
p {
color: <?php echo $azul; ?>;
margin:50px;
}
index.php
<link rel="stylesheet" href="./css.php" />
<p>Azul</p>
Although that will work, it would not be entirely correct because the server would be returning PHP instead of CSS (which could lead to some kind of compatibility problems). To avoid this, you should add a line to the beginning of css.php
to indicate that the content returned is CSS:
header('Content-type: text/css');
With that you already get what you are looking for but, as in the television commercials: " there is still more! ". That works, yes; but users can see that you are serving CSS through PHP (because they can see the extension of the file) and it is information that they do not need to know (the less information users and "bad guys" have about the architecture of your website , better).
Therefore, as a last step (not necessary), I would recommend that you update the redirection to mask css.php
as if it were a real .css file. Something like this in .htaccess
:
RewriteEngine On
RewriteRule css.css css.php [L]
Then your PHP will be used as CSS, and in addition the users will see it as if it were a real CSS file (although internally it is PHP).
Thus, the two initial files, plus the change in .htaccess
indicated above, would look like this:
css.php
<?php
header('Content-type: text/css');
$azul = "blue";
?>
p {
color: <?php echo $azul; ?>;
margin:50px;
}
index.php
<link rel="stylesheet" href="./css.css" />
<p>Azul</p>
.htaccess
RewriteEngine On
RewriteRule css.css css.php [L]