Some time ago, in a project, any div background color value on the same page must be automatically generated using php. That is to say, each div generates a different color. Method 1:
Generate a random color value (for example, FF00FF ).
Color. php
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:
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:
The Code is as follows:
Function randColor (){
$ Colors = array ();
For ($ I = 0; $ I <6; $ I ++ ){
$ Colors [] = dechex (rand (0, 15 ));
}
Return implode ('', $ colors );
}
The usage is as follows:
<? Php echo 'random color: # '. randColor (). '';?>