有没有办法PHP可以控制CSS

1) database

2) php

3) css

4) html form

The user enters some properties for example background: red in the html form and it saves in the db, then php loades from the db the color option in background css... and then how can I put php variable in the css properties...

also some hostings does not allow to add handlers in .htaccess, is there other options?

Yes, it is possible.

You can serve a css file with php by setting the content-type.

<?php header('content-type: text/css; charset=utf-8'); ?>

You would output the css from the php file.

You could store values in a mySQL or other table with a form, and later retrieve them with the mySQLi or other library.

This is an example of the php script:

<?
    header('content-type: text/css; charset=utf-8');

    $bgColor = "#FFF"; // Get it from database, I'm setting manually for this example
?>

body{
      background-color:<?php echo "$bgColor" ?>;
}

Alternatively, you could output inline css on the page. Either directly in to a tag, or inline on elements.

If really needed a native css file for some reason (I can't think of one), you could technically overwrite a specific css file using php's fwrite() function, though I wouldn't recommend doing it this way.

 <?php

      $filename = "phpstyle.css";

      $fp = fopen($filename, 'w');

      $bgcolor = "#FFF";

      $css  = "body{";
      $css .= "background-color:".$bgcolor.";"; 
      $css .= "}";           

      fwrite($fp, $css);

      fclose($fp);

 ?>

Yes you can serve a css through php script.

One method is to output style elements through php.

<?php
    #css on php file
    $color = '#FFFFFF';
?>
<html>
<head>
    <title>Css through php</title>
    <style>
        .test{
            text-color: <?php echo $color;  ?> 
        }
    </style>
</head>
<body>
<p class="test">Your body </p>
</body>
</html>

Another method is to generate css dynamically in a php file and linking it on an html page.

<?php
    #css on php file [test_css.php]
    header("Content-type: text/css");
    $color = '#FFFFFF';
?>
.test{
            text-color: <?php echo $color;  ?> 
}

<html>
<!-- test.html -->
<head>
    <title>Css through php</title>
    <link rel="stylesheet" type="text/css" href="test_css.php">
</head>
<body>
<p class="test">Your body </p>
</body>
</html>

ether create a css file using php dynamically, such as:

<link href="../styles/styles.php" type="text/css" />

or use php to write the css inline on your html file:

<html>
<head>
<style type="text/css">
body {
   background-color: <?php echo $redcolor; ?>;
}
</style>
</head>
<body></body>
</html>

however, you cannot create a standard css file with php.

In addition to Khaleel's answer, you can put CSS in the HTML document itself, in a

<style>
...
</style>

section. You can use PHP variables to fill this in.

Or you can use inline styles in the HTML

<span style="...">...</span>

Why not just:

<?php if (myCondition) { ?>
<style>.myClass {
background: <?php echo $myColor; ?> !important;
} </style>
<?php } ?>

The myCondition triggers the insertion of in body styling. The !important tag overwrites all other existing value of that property.

Simple to understand and easy to use. Works every time.