6

I have a css file named test.css and I want to use into it of $var.$var is at test.php. test.css is attached in test.php. My structure is something like this:

//test.php

<html>
<head>
<?php $var = 'anything';?>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
</html>

and this is test.css:

// test.css

.<?php echo $var> { // css property }

Currently test.css does not work. In fact, I want to knoe how can I use of a php variable as a class name into a css file ?

  • as written, impossible. css is not executed as a php script, so any php code in the css will go out to the client and be considered a css syntax error. you CAN make it execute as php on the server, but then it will execute completely SEPARATELY from the script that's producing the `` tag, and `$var` will be undefined in the css. – Marc B Jul 03 '15 at 21:03
  • I also add another solution for you. If you think that is the answer, please change your decision for new comers. – hakki Jul 03 '15 at 21:33
  • @MarcB when I put a `.css` file in head, It will be cache in user browser. but what happens when I write my css property on `head` between `` ? what happens with each request ? is it optimized ? –  Jul 03 '15 at 21:35
  • then you're wasting a bit of bandwidth by sending the same css snippet on every one of your pages. it'd get cached as part of the page, but since it's duplicated on ALL pages, it'd be cached mutiple times. – Marc B Jul 03 '15 at 21:48
  • @MarcB look, for preventing of parsing my data in html page, I use dynamic class name for my elements, and I create these names with php, now in your opinion, is it bad a idea ? –  Jul 03 '15 at 21:53
  • rather pointless, since you don't NEED classes to parse html. they make it easier, but you can still yank apart html purely by the structure. `3rd table, 4th row, 3rd cell, 2nd paragraph`, works just as well as `$('.thing_you_want')`. – Marc B Jul 03 '15 at 21:54
  • @MarcB I thought about that already. I will `echo` html tags as random between my main tags. `
    for preventing parser
    main data
    `. there is just sometime the first `
    `.
    –  Jul 03 '15 at 22:01
  • @MarcB can I chat with you [here](http://chat.stackoverflow.com/rooms/82340/room-for-stack-and-marc-b) a little ? –  Jul 03 '15 at 22:18

2 Answers2

12

Actually you can.

1st Solution

Instead of using the .css file extension, use .php

Set up variables

<?php

   header("Content-type: text/css; charset: UTF-8"); //look carefully to this line

   $brandColor = "#990000";
   $linkColor  = "#555555";
?>

Use variables

#header {
   background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
  color: <?php echo $linkColor; ?>;
}

...

ul#main-nav li a {
  color: <?php echo $linkColor; ?>;
}

2nd and short solution

Create a file and name it like style.php, then in your style.php set your styles in tags like below

style.php

<style>
.blabla{
   ....
}

#heeeHoo{
   ...
}
</style>

then include style.php to your file (test.php) like

<html>
<head>
    <?php include 'style.php'; ?>
</head>
<body>

</body>
</html>

That is the correct answer. Think like inline css but that is actually in external file

hakki
  • 6,181
  • 6
  • 62
  • 106
  • 1
    your Idea is great ! +1 for you. –  Jul 03 '15 at 21:27
  • `include` or `require` or `include_once` or `require_once` ? which one is more optimized ? –  Jul 03 '15 at 21:35
  • Please see this answer for your new question: http://stackoverflow.com/a/3546316/1848929 – hakki Jul 03 '15 at 21:37
  • then `require_once()` is the best in this case. –  Jul 03 '15 at 21:39
  • You should `mod_rewrite` `style.css` to `style.php` though imho – DarkBee Jul 03 '15 at 21:42
  • If I use `require_once();` in head, it will be run before html tags ? –  Jul 03 '15 at 21:48
  • Just use `` and in your `.htaccess` file you can `rewrite` `style.css` to `style.php`. This way the browser can take advantage of caching the resource. – DarkBee Jul 03 '15 at 21:51
  • no. require_once is kind of strong include. if it fails, page stop working but if you use include page continues working. – hakki Jul 03 '15 at 21:52
0

You can use <style> in the PHP file.

//test.php

<html>
<head>
<?php $var = 'anything';?>
</head>
<body>
<style>
<?php echo $var; ?>
</style>
</body>
</html>

The style can also be put in the <head>.

bnahin
  • 796
  • 1
  • 7
  • 20
  • Then you'd have to configure your server to treat your `css` files as PHP files. – mittmemo Jul 03 '15 at 21:01
  • @mittmemo is it possible ?! and is it hard ? –  Jul 03 '15 at 21:03
  • @rjdown what ? what is your mean of *Most browsers don't understand scoped* , using ` –  Jul 03 '15 at 21:04
  • Parsing CSS as PHP is Ill-advised. Just use a PHP file in your link. And @stack I was referring to the unedited answer http://stackoverflow.com/revisions/31214136/1 - but yes, using inline styles is generally a bad idea, especially half-way through the document. – rjdown Jul 03 '15 at 21:05
  • @stack I had included the "scoped" attribute in my original answer and edited it out. rjdown saw that before it was edited out. – bnahin Jul 03 '15 at 21:06