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?
3 Answers
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.
1 Comment
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)