Generate a Random Hex Colour in PHP
Posted by matthew | Posted in Code Snippets | Posted on 01-12-2009
Tags: PHP
0
Often required for testing this simple function will generate a random hexadecimal colour every time.
Copy this piece of code into your page:
function newColour() {
$swatch = array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","F");
$rand_colours = array_rand($swatch,5);
$colour = "#". $swatch[$rand_colours[0]] .$swatch[$rand_colours[1]] . $swatch[$rand_colours[2]] . $swatch[$rand_colours[3]] . $swatch[$rand_colours[4]] . $swatch[$rand_colours[5]];
return $colour;
}
You can choose to echo the result or use it as you see fit.
echo newColour();



