我意识到这种类型的问题已经有几个线程,但似乎没有一个与我的匹配。我有正确显示 CSS 背景的一系列 6 个数字的代码。然后将这 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 才能被引用?如果我这样做,我该怎么做?
非常感谢!