1

I'm trying to use php variables in my .css. I have change my style.css to style.php and I've add the next code in it:

 <?php header("Content-type: text/css");


 $color[0]='#ff0000';
 $color[1]='#00ff00';
 $color[2]='#0000ff';


 $i=rand(0,2);
 ?>

Then I'm trying to use this color in a css property. Something like this:

 background-color: "<?=$color[$i]?>";

When I try to see what happens... nothing happens. If I see on firefox inspector it seems that my code is not changing from php to html.

Any Ideas?

9
  • Where are You putting background-color: "<?=$color[$i]?>";? Commented Nov 3, 2015 at 16:09
  • Seems to work fine for me - does your server definitely allow shortened PHP tags (<?)? Commented Nov 3, 2015 at 16:12
  • 2
    This doesn't directly answer your question but have you looked at using a CSS pre-processor like LESS or SASS? Commented Nov 3, 2015 at 16:13
  • He's only using the short tag with echo.. <?= .. and that's always valid even if short tag is off in php.ini Commented Nov 3, 2015 at 16:13
  • 1
    @MagnusEriksson, if the php version is below 5.4 it's a problem if short tags is off Commented Nov 3, 2015 at 17:20

3 Answers 3

1

Use classes on body:

<?php 
    $class[0]='red';
    $class[1]='lime';
    $class[2]='blue';
    $i=rand(0,2);
?>

<body class="<?php echo $class[$i]; ?>">

//The result
<body class="red"></body>
<body class="lime"></body>
<body class="blue"></body>

Work with inheritances on CSS file:

.red .my-another-class{
    background-color: red;
}
.lime .my-another-class{
    background-color: lime;
}
.blue .my-another-class{
    background-color: blue;
}
Sign up to request clarification or add additional context in comments.

Comments

0

So if you're loading style.php as a CSS file you should do this in your <head> so it's treated by the browser as such

<link href="style.php" rel="stylesheet">

Comments

0

IMPORTANT:

I'm trying to use php variables in my .css

You CAN NOT use php in .css files, your "css" file has to be a php file, look the example above.

In PHP: Just remove the quotes from your output:

 background-color: <?=$color[$i]?>;

If your PHP version is below 5.4 and short open tag is disabled, you have to write it like this:

background-color: <?php echo $color[$i] ?>;

From the PHP docs:

Note: This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo. Use of this shortcut required short_open_tag to be on. Since PHP 5.4.0, <?= is always available.

EXAMPLE

In this example index.html and css.php are on the same level

htdocs
   |-- index.html
   |-- css.php

index.html:

<html
    <head>
        <link rel="stylesheet" type='text/css' href="css.php" />
    </head>
    <body>
        Sometext
    </body>
</html>

your css.php:

header("Content-type: text/css; charset: UTF-8");
$color[0]='#ff0000';
$color[1]='#00ff00';
$color[2]='#0000ff';


$i=rand(0,2);
?>
body {
    background-color: <?=$color[$i]?>;
}

3 Comments

Why is it not working, how do you use it, can you add your example files to your answer?
firefox inspector shows me background-color: <?=$color[$i]?>; and not the color inside the variable. And obviously, color does not change...
If you still have the problems, I would recommend you to update your question, add a small example like I did, I think there is some other error, you can try to change it to <?php echo $color[$i] ?>Maybe your PHP version is too old

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.