Grant_Bailey
Grant_Bailey

Reputation: 308

If ... else conditional statements in CSS stylesheet processed as PHP

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.

Upvotes: 1

Views: 2167

Answers (2)

Grant_Bailey
Grant_Bailey

Reputation: 308

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.

Upvotes: 1

pardeep
pardeep

Reputation: 21

use this

help you out.......

`

echo "'* {'";

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

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

echo "'}'";

?>`

Upvotes: 0

Related Questions