Random color generator


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function randomColor() {
  2. $str = '#';
  3. for($i = 0 ; $i < 6 ; $i++) {
  4. $randNum = rand(0 , 15);
  5. switch ($randNum) {
  6. case 10: $randNum = 'A'; break;
  7. case 11: $randNum = 'B'; break;
  8. case 12: $randNum = 'C'; break;
  9. case 13: $randNum = 'D'; break;
  10. case 14: $randNum = 'E'; break;
  11. case 15: $randNum = 'F'; break;
  12. }
  13. $str .= $randNum;
  14. }
  15. return $str;
  16. }
  17. $color = randomColor();
  18. echo '<span style="color:'.$color.'">Random color: '.$color.'</span>';

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.