0

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

2
  • To be able to use PHP-variables in other files, you need to use include, require or require_once. Putting it like you do in the style, it will be a separate request to the server and there off totally sandboxed from each other. Commented Feb 15, 2017 at 6:39
  • why you don't use include('css/style.php') some thing like that Commented Feb 15, 2017 at 6:46

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

No, I don't want to pass those data through GET or POST variables.
@user7566909 "I don't want to..."... could you elaborate on why not?

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.