The Hack o blog

Reinventing the weel

PHP – Random Hex Colour

Vector logo of the PHP programming language wi...

Image via Wikipedia

Something useful I think anyone who uses PHP might like… this small function will return a random Hex value which converts to a colour (I.E. #FF0000 = Red, #00FF00 = Green, #0000FF = Blue etc). It’s small but it might come in handy one day so take a note of it!

<div style="border: 1px solid #FFF; background: #DDD; color: #000; padding-left: 15px;"><code><?php
function random_color() {
for ($i = 0; $i<6; $i++) {
$c .= dechex(rand(0,15));
}
return "#$c";
}
echo random_color();
?></code></div>
<div style="border: 1px solid #FFF; background: #DDD; color: #000; padding-left: 15px;">
Advertisements

2 responses to “PHP – Random Hex Colour

  1. TheZre0 February 26, 2010 at 11:37 PM

    hy man extant blog thanks for the help

  2. Bubbi October 21, 2011 at 8:50 AM

    Superior thinking dmoentsrtaed above. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: