Get Random Colour in PHP

G

Ever wanted to get random colours for your PHP scripts? This easy randomise script does just that – It randomly picks a few colours for you!

<?
//randomise the RGB colours
$r = rand(128,255);
$g = rand(128,255);
$b = rand(128,255);
//sets the colour code in 000000 format
$colour = dechex($r) . dechex($g) . dechex($b);
?>

That’s it! Then just use $colour in your PHP code. The dechex() method just converts the number to the hexadecimal format that you need.

I used half the colour spectrum (from 128 to 255) so that only the brighter colours will be generated. If you’d prefer, you can use the full spectrum (from 0 to 255) or the darker spectrum (from 0 to 128).

Have fun!

Similar Posts:

About the author

Alvin Poh

I've over 17 years of entrepreneurship experience, having built a tech company to be the top in the country, which was subsequently acquired for an 8-figure sum. As CEO, I led the company through robust growth to 150 employees across 4 countries. After the acquisition, I embraced minimalism, sold off my personal possessions, and started living around the world.

Read more about my story here.

Add comment

By Alvin Poh

About Author

I've over 17 years of entrepreneurship experience, having built a tech company to be the top in the country, which was subsequently acquired for an 8-figure sum. As CEO, I led the company through robust growth to 150 employees across 4 countries. After the acquisition, I embraced minimalism, sold off my personal possessions, and started living around the world.

Read more about my story here.