Home > php教程 > php手册 > body text

php生成随机颜色方法汇总

WBOY
Release: 2016-06-06 20:16:13
Original
1389 people have browsed it

前段时间在一个项目中,需要在同一个页面中的任意div背景颜色值,用php自动生成,也就是说每个div都产生不同的颜色

方法一:
随机生成颜色值(例如 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().'';?>

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!