If you want to create a random background color
and then you’d use the following in your body tag:
<body BGCOLOR=“#<?php echo($hex);?>”>
Writing about PHP scripting since 12/99. Learn something new every day.
PHP Diary // Home
Friday, July 14th, 2006
will this be able to call upon a certain (set of colors) if certain variables are entered. I’m working on a site and i need help for backend coding
on August 18th, 2006 at 8:33 am | #Link CommentHandy code but you have a problem if rand returns 16 or less as the HEX will then only be one character long, breaking the color. You can either choose to str_pad each one like so:
str_pad(dechex(rand(0,255)),1,dechex(rand(0,16)))
or, a bit less bulky, you can just set the lower limit to 17:
dechex(rand(17,255))
That way you don’t get a true random color as the lower values will never come out. But not having the darkest of colors can also work out for the best, especially when using it as a background color.
on September 8th, 2007 at 8:40 pm | #Link CommentSteve is right, but it might be easier to divide it into 6 numbers 0 - F, since only the first zero is the problem, and there’s really no reason to subdivide a six digit number into three groups of two (unless you later want to have the ability to work with individual RGB color values). By concatenating the random one-digit zero, it stays put. I.e., 07 = 7, but 0 = 0 always.
$color = dechex(rand(0,15)).dechex(rand(0,15)).dechex(rand(0,15)).dechex(rand(0,15)).dechex(rand(0,15)).dechex(rand(0,15));
(Note that “15″ is 16 digits including zero, the same way that 255 is 256 incl. zero.)
I’m sure you could further simplify it by dividing it into 1 (0-15) and 5 (0-1048575) digit random numbers.
on August 13th, 2008 at 2:31 am | #Link Commentfor readability:
$color = dechex(rand(0,15)) . dechex(rand(0,15)) . dechex(rand(0,15)) . dechex(rand(0,15)) . dechex(rand(0,15)) . dechex(rand(0,15));
on August 13th, 2008 at 2:32 am | #Link Comment< b >< a href=”http://www.job-run.com/wikka/LisaAiuq” >bob hairstyles< /a >< /b >< /blockquote >
I like the helpful info you provide in your articles. I will bookmark your weblog and check again here regularly. I’m quite certain I’ll learn plenty of new stuff right here! Good luck for the next!
on February 5th, 2012 at 11:40 pm | #Link Comment