Jacky A.
Jacky A.

Reputation: 21

get back PHP variable from php CSS file

My problem is that I cannot access a PHP variable from a CSS file loaded like this from index.php:

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

In the style.php file, I have this:

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

$myClassName = 'myClass'; ?>

.<?= $myClassName?> {
    font-weight: bold;
}

in my index.php I have this:

<span class='<?= $myClassName?>'>this is a text</span>

But $myClassName return an empty string like it doesn exists...it does mean that I cannot access the PHP variable like this....is there someone have maybe a trick..?

I really need to set the css classnames with PHP variables from the css file and be able to get them back to my index.php

Upvotes: 0

Views: 93

Answers (1)

user6288471
user6288471

Reputation:

Under the header, do $css = $_GET['css']; or replace it with wherever you are initializing the variable from.For example:

<?php 
    header('Content-Type: text/css'); 
    $css = $_GET['css']; 
?>
body {
    <?= $css ?>border-radius: 3px
}

Upvotes: 1

Related Questions