PHP: Select Random Web Color with One Line of Code

HTML or Web colors are defined using a hexadecimal (hex) notation for the combination of Red, Green and Blue color values, commonly known as RGB. The lowest color value possible for one of these “light” sources is 0 (hex 00) and the highest value is 255 (hex FF). The color codes are written as 3 double digit numbers prefixed with a # sign. Technically, the combination of 255 x 255 x 255 possible color codes means that there exists 16 million possible colors using this particular notation.

The quickfire tip for today is the generation of a random HTML color using just a single line of PHP color. Handy if you want to throw a bit of a surprise burst of changing color here and there…

$randomcolor = '#' . strtoupper(dechex(rand(0,10000000)));

The line above will produce a value that looks something like #240BFD, perfect for slotting in any web element’s style you can think of!

Simple, wasn’t it? ;)

Related Posts:

  • Although I've posted simple random color generating code snippets before, I've noticed since that most of them have a flaw in that they sometimes don't always produce a proper six character long hex color code. Hence I have posted my latest random color generating PHP function to these pages in the interest of rem ...

  • Gradient colors are pretty awesome, pretty to look at and a nifty visual effect. Unfortunately for us though, CSS standards when it comes to handling gradients was pretty lacklustre over the years, resulting in all the major browsers pretty much defining their own browser specific CSS rules to achieve gradient ...

  • Sometimes you need to RGB array that makes up a color when working in the wonderful world of PHP. Don't ask me why or when, though that said, I have required it at least once before when setting text color whilst generating PDFs using the FPDF library - so I have at least established that you do sometimes want to d ...

  • From jQuery 1.2 onwards, the ability to tween (animate) from one color to another, in other words fade one color into a different color is as simple as inserting one line of code. (In order to use this effect though, you will need to include the official Color Animations jQuery plugin which can be grabbed here.) ...

  • Firefox has a nasty habit of returning element background colors and colors in general in the format rgb(xxx,yyy,zzz) as opposed to the normal #xxyyzz hexadecimal format we've been accustomed to seeing from the likes of Internet Exlorer and the rest of its browser ilk. Obviously this can trip up a fair bit of ...

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.

  • http://blog.sven.co.za/ Sven Welzel

    Quick heads-up: Surely you meant strtoupper for this PHP example (rather than the VB script ucase that would use the & as concatenation character…?
    .-= Sven Welzel´s last blog ..Windows cannot find FILENAME from Outlook or Thunderbird – Make sure you typed the name correctly, and then try again. =-.

  • http://blog.sven.co.za Sven Welzel

    Quick heads-up: Surely you meant strtoupper for this PHP example (rather than the VB script ucase that would use the & as concatenation character…?
    .-= Sven Welzel´s last blog ..Windows cannot find FILENAME from Outlook or Thunderbird – Make sure you typed the name correctly, and then try again. =-.

  • http://www.craiglotter.co.za Craig Lotter

    Haha oops, so I did! Thanks for picking up on it Mr. Sven – I’ve fixed it appropriately. (Sigh, the amount of times I get my syntax mixed up between the various languages…)

  • http://www.craiglotter.co.za Craig Lotter

    Haha oops, so I did! Thanks for picking up on it Mr. Sven – I’ve fixed it appropriately. (Sigh, the amount of times I get my syntax mixed up between the various languages…)

  • http://web2clone.com/ Saroj

    nice, Thanks

  • Scott Rowley

    Exactly what I was looking for.  thanks!

    • http://www.craiglotter.co.za Craig Lotter

      Excellent, glad I could assist! :)

  • Tijo

    super

  • Sale

    Great blog, thanks for the tip!