Use php to generate random color method code finishing

Use php to generate random color method code finishing

method one

<?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>

Method Two

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; 
} 

Method Three

function randColor(){
    
    
    $colors = array();
    for($i = 0;$i<6;$i++){
    
    
        $colors[] = dechex(rand(0,15));
    }
    return implode('',$colors);
}

The method of use is as follows:

<?php echo '<span style="color: #'.randColor().'">随机颜色:#'.randColor().'</span>';?>

Guess you like

Origin blog.csdn.net/guo_qiangqiang/article/details/113766185