Generate A Random Colour With PHP

Here is a short bit of code to generate a random hexadecimal colour using PHP. Essentially you just create a random number between 0 (000000) and 10,000,000 (ffffff) and turn this into a hexadecimal number using the PHP function dechex.

$colour = rand(0, 10000000);
$colour = dechex($colour);

This can also be accomplished on a single line.

$colour = dechex(rand(0, 10000000));

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
11 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.