让PHP更改随机生成的CSS背景颜色


Getting PHP to change a randomly generated CSS background color

我意识到已经有几个关于这类问题的线程,但似乎没有一个与我的线程相匹配。我的代码正确地显示了一系列6个数字的CSS背景。然后,这6个数字被存储到一个数组中,该数组的内容用前面的"#"符号连接。然后,它被存储为另一个变量。此处:

$v = 0;
$array = array();
$tot;
$other0 = null;
$other1 = null;
$other2 = null;
$other3 = null;
$other4 = null;
$other5 = null;
$color;
for ($i=0;$i<6;$i++){ //loops 6 times to create 5 numbers
    $tot = rand(0, 15);
    if (($tot>9) && ($tot<16)){ //assigns 10 to "a" in hex
        if ($tot == 10){
            $tot = $other0;
            $other0 = "a";
            //echo $other0;
            $array[$v++] = $other0;
        }
        elseif ($tot == 11){ //assigns 11 to "b" in hex
            $tot = $other1;
            $other1 = "b";
            //echo $other1;
            $array[$v++] = $other1;
        }
        elseif ($tot == 12){ //assigns 12 to "b" in hex
            $tot = $other2;
            $other2 = "c";
            //echo $other2;
            $array[$v++] = $other2;
        }
        elseif ($tot == 13){ //assigns 13 to "b" in hex
            $tot = $other3;
            $other3 = "d";
            //echo $other3;
            $array[$v++] = $other3;
        }
        elseif ($tot == 14){ //assigns 14 to "b" in hex
            $tot = $other4;
            $other4 = "e";
            //echo $other4;
            $array[$v++] = $other4;
        }
        elseif ($tot == 15) { //assigns 15 to "b" in hex
            $tot = $other5;
            $other5 = "f";
            //echo $other5;
            $array[$v++] = $other5;
        }
    }
    else {
        //echo $tot;
        $array[$v++] = $tot; //if not, then just assign to array
    }
}
$var = "";
$color = "";
for ($t=0; $t<6;$t++){
    $var .= (string) $array[$t]; //displays the 6 numbers as one string
}
//var_dump($var); //for more visual reference, uncomment the var_dump
$color = "#" . $var;
echo $color;

上面是用PHP编写的。

如何使用CSS显示该变量?它像(在同一个PHP文件中):echo "<style>color:$color</style>";

还是我必须制作一个style.php才能被引用?如果我这样做了,我该怎么做?

非常感谢!

看起来"<style>color:$color</style>";将输出损坏的html/css。

尝试修改

echo '<style type="text/css"> body { background: '.$color.' } </style>';

应该是

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

这需要在您的<head>元素中。

<body style="background-color:<?php echo $color; ?>;">

content

</body>

style属性允许您为元素定义自定义css,css属性,background color基本上定义了该元素的背景颜色。。。不言自明,我知道!

为什么不看看这些链接呢。

使用php 更改页面的背景色

因此,与其这么做:

if blablabla {
    echo '<body style="background-color:white">'; 
}
else {  
    echo '<body style="background-color:orange">'; 
} 

这样做:

echo '<body style="background-color:(variable)">'; 
<?php
    $alpha = ["a","b","c","d","e","f","0","1","2","3","4","5","6","7","8","9"];
    $color = "#";
    for ($i=0; $i<6; $i++) {
        $index = rand(0,count($alpha)-1);
        $color .= $alpha[$index];
    }
?>
<html>
    <body style="background:<?php echo $color ?>">
        <?php echo $color ?>
    </body>
</html>

你似乎让事情变得比实际情况复杂了一点。下面是一个html集成的示例,正如你最初所要求的。:)

声明数组中需要的所有元素,然后循环6次:生成一个随机索引,从数组中获取相应的元素,并将其连接到颜色变量。

最后,如前所述,您可以在html文件中的任何需要的地方回显它。