IT & Programming

Get random hexadecimal color code with php

Custom PHP function below randomly generates and returns a hexadecimal color code.

Function

function color() {
        
      return '#'.str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT).str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT).str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
}

Calling The Function

echo color();

Output

#00ff00

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *