Wednesday, March 31, 2010

PHP Random Color

This kind of works okay!


<?php $hexColor=dechex(rand(0,255)).dechex(rand(0,255)).dechex(rand(0,255)); ?>
<div style="height: 100px; width: 100px; background: #<?php echo $hexColor; ?>">
</div>


// thius one works a little better

<?php $hexColor=str_pad(dechex(rand(0,255)), 2, "0", STR_PAD_LEFT).str_pad(dechex(rand(0,255)), 2, "0", STR_PAD_LEFT).str_pad(dechex(rand(0,255)), 2, "0", STR_PAD_LEFT); ?>

No comments:

Post a Comment