1

I'm processing a CSS stylesheet as PHP by including the following at the top of my CSS file:

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

My CSS stylesheet (style.css) is called like this:

<style type="text/css">@import url("style.css") only screen and (min-device-width: 321px), only print;</style>

I have altered my Apache config file in the following way to enable the CSS file to be processed by the PHP engine:

<FilesMatch "^.*?common.*?$">
SetHandler php5-script
</FilesMatch>

In my CSS file, beneath the header, is regular CSS content with the occasional php snippet, e.g.

...

<?php echo "* {margin: 10px; padding: 10px;}"; ?>

html
{
    background-color: #FFF;
    overflow-y: scroll;
}

The above works fine, however if I try to include a php conditional statement I run into problems. For example, the following:

<?php
echo "* {";
if ($fixed_navigation_Home == "none") {echo "display: none;";}
else {echo "display: block;";}
echo "}";
?>

... produces only:

* {
}

... instead of one of the two style rules requested in the PHP.

Is anyone able to advise me what might be going wrong? Thanks for reading.

5
  • Do you've this $fixed_navigation_Home variable? Commented Jun 11, 2013 at 9:42
  • Yes, it's: $fixed_navigation_Home = (string) "none"; Commented Jun 11, 2013 at 9:47
  • Try refreshing the browser cache ;) Commented Jun 11, 2013 at 9:52
  • Yes, did that a number of times. Also, tried <link> instead of <style>, tried renaming css file to style.php, without success. Commented Jun 11, 2013 at 10:19
  • code is ok so clear your cashe ctrl+shfit+del or ctrl + F5. :) Commented Jun 11, 2013 at 12:06

2 Answers 2

1

Oops ...

The problem was my variable $fixed_navigation_Home which was declared by a controller file. Thanks to Mr Alien for drawing my attention to it.

The problem, I now realise, is that web pages are stateless: once the PHP processor has processed the page variables are destroyed. Consequently, my variable was not being 'remembered' since the style sheet is called by the browser after the page is generated.

Declaring the variable as a global did not help, for the reason given above. For those interested in the issue there are other pages which explain how to resolve the 'pass variable between pages' problem:

https://stackoverflow.com/questions/9726352/using-php-variables-across-different-files

Number of ways to share a variable defined in one PHP file among all the files of a website / application?

Using PHP variables on multiple pages

Thanks for the responses.

Sign up to request clarification or add additional context in comments.

Comments

0

use this

help you out.......

`

echo "'* {'";

if ($fixed_navigation_Home == "none") {echo "'display: none;'";}

else {echo "'display: block;'";}

echo "'}'";

?>`

Comments

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.