방법 1
<?php
function random_color(){
mt_srand((double)microtime()*1000000);
$c = '';
while(strlen($c)<6){
$c .= sprintf("%02X", mt_rand(0, 255));
}
return $c;
}
$hao = '#'.random_color();
?>
<b style="background-color:<?php echo $hao; ?>">颜色</b>
방법 2
function randrgb()
{
$str='0123456789ABCDEF';
$estr='#';
$len=strlen($str);
for($i=1;$i<=6;$i++)
{
$num=rand(0,$len-1);
$estr=$estr.$str[$num];
}
return $estr;
}
방법 3
function randColor(){
$colors = array();
for($i = 0;$i<6;$i++){
$colors[] = dechex(rand(0,15));
}
return implode('',$colors);
}
사용 방법은 다음과 같습니다.
<?php echo '<span style="color: #'.randColor().'">随机颜色:#'.randColor().'</span>';?>