What is the easiest way to set background color in PHP ?
<?php
header('Content-Type: text/css');
?>
some selector {
background-color: <?php echo $my_colour_that_has_been_checked_to_be_a_safe_value; ?>;
}
You can use php in the style sheet. Just remember to set header("Content-type: text/css") in the style.php (or whatever then name is) file
This really depends on what you need to do. If you want to set a background colour on a page then you need to use CSS as per Jay's and David Dorward's answers.
If you are building an image with PHP then you can use the GD library to allocate colours yourself. I don't recommend this without thoroughly reading up on how to create images with GD. http://www.php.net/manual/en/function.imagecolorallocate.php
Try this:
<style type="text/css">
<?php include("bg-color.php") ?>
</style>
And bg-color.php can be something like:
<?php
//Don't forget to sanitize the input
$colour = $_GET["colour"];
?>
body {
background-color: #<?php echo $colour ?>;
}
You better use CSS for that, after all, this is what CSS is for. If you don't want to do that, go with Dorwand's answer.
I would recommend to use css, but php to use to set some class or id for the element, in order to make it generated dynamically.