有时我们需要随机的颜色,如本站的标签背景色,小指分享两种方法。
方法一:完全随机的颜色<?php
function randColor(){
$colors = array();
for($i = 0;$i<6;$i++){
$colors[] = dechex(rand(0,15));
}
return implode('',$colors);
}
?>
使用颜色的方法:<?php echo '随机颜色:#'.randColor().'';?>
方法二:随机挑选数组里的颜色<?php
function randColor(){
$colors=array('5CB85C','428BCA','FF6600','D9534F','B37333','00ABA9');
$show_color = array_rand($colors, 1);
return $colors[$show_color];
}
?>
使用颜色的方法是一样的,但是只会是你设置的数组里面的颜色:<?php echo '随机颜色:#'.randColor().'';?>
需要的自己试试吧~