0

How can I have certain CSS load only if a specific PHP variable is true and have other CSS load of the variable is false?

1
  • I think you have answered your own question Commented Mar 18, 2013 at 0:01

3 Answers 3

2

Have it in 2 separate files and have the correct file load depending on the boolean state or use if statements to echo the code you want within the document depending on the boolean state.

<style type='text/css'>
  <?php
    if(boolean)
    {
      echo "elementName { attribute: value; } ";
      echo "elementName2 { attribute: value;} ";
    }
    else
    {
       //echo css here
    }
  ?>
</style>

or, in the head tag:

<?php

    if (boolean)
    {

      echo "<link rel='stylesheet' type='text/css' href='1.css'>";


    }
    else
    {

      echo "<link rel='stylesheet' type='text/css' href='2.css'>";

    }
?>

Also, form vs content; it would be more practical to keep your CSS in a separate file and have a particular CSS script load depending on a variable rather than mix up the CSS with the HTML.

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

1 Comment

How do you echo just a specific line of CSS within PHP?
2
<link rel="stylesheet" type="text/css" href="<?php echo ($var) ? "css1" : "css2"; ?>.css">

Comments

-1

In your HTML page, on the <head> section, you can write some simple conditions in PHP to include or not a CSS:

<?php

    if ($yourCondition)
    {
?>
    <link rel="stylesheet" type="text/css" href="http://path/to/css1.css" />

<?php
    }
    else
    {
?>
    <link rel="stylesheet" type="text/css" href="http://path/to/css2.css" />
<?php
    }

Note: Avoid using inner style, because all style should be in CSS (presentation layer separated from the HTML which is a structure and data layer)

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.