Heim  >  Artikel  >  Backend-Entwicklung  >  php生成随机颜色方法汇总_php实例

php生成随机颜色方法汇总_php实例

WBOY
WBOYOriginal
2016-06-07 17:14:581258Durchsuche

方法一:
随机生成颜色值(例如 FF00FF).

color.php

复制代码 代码如下:

function random_color(){
    mt_srand((double)microtime()*1000000);
    $c = '';
    while(strlen($c)         $c .= sprintf("%02X", mt_rand(0, 255));
    }
    return $c;
}

方法二:

复制代码 代码如下:

function randrgb() 

  $str='0123456789ABCDEF'; 
    $estr='#'; 
    $len=strlen($str); 
    for($i=1;$i     { 
        $num=rand(0,$len-1);   
        $estr=$estr.$str[$num];  
    } 
    return $estr; 
}

方法三:

复制代码 代码如下:

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

使用方法如下:
随机颜色:#'.randColor().'';?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn