Home  >  Article  >  Backend Development  >  Summary of methods for generating random colors in php, summary of php generation_PHP tutorial

Summary of methods for generating random colors in php, summary of php generation_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:12:26875browse

A summary of methods to generate random colors in php, a summary of php generation

Method 1:
Randomly generate color values ​​(e.g. FF00FF).

color.php

Copy code The code is as follows:

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

Method 2:

Copy code The code is as follows:

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 3:

Copy code The code is as follows:
function randColor(){
$colors = array();
for($i = 0;$i<6;$i++){
          $colors[] = dechex(rand(0,15));
}
Return implode('',$colors);
}


How to use it:

Random color: #'.randColor().'';?>


http://www.bkjia.com/PHPjc/920970.html

truehttp: //www.bkjia.com/PHPjc/920970.htmlTechArticleA summary of PHP methods to generate random colors, PHP generation summary method one: Randomly generate color values ​​(such as FF00FF). color. The php copy code is as follows: function random_color(){ mt_srand((doubl...
Statement:
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
Previous article:PHP Generate RSS File Class Example_PHP TutorialNext article:PHP Generate RSS File Class Example_PHP Tutorial

Related articles

See more