Home  >  Article  >  Backend Development  >  How to implement hexadecimal color random generator function in PHP

How to implement hexadecimal color random generator function in PHP

墨辰丷
墨辰丷Original
2018-05-19 09:35:471626browse

This article mainly introduces the PHP hexadecimal color random generator function, and analyzes the relevant operating skills of PHP to randomly generate hexadecimal numerical representations based on specific examples. Friends in need can refer to the following

The example in this article describes the PHP hexadecimal color random generator function. Share it with everyone for your reference, the details are as follows:

<?php
function randomColor() {
  $str = &#39;#&#39;;
  for($i = 0 ; $i < 6 ; $i++) {
    $randNum = rand(0 , 15);
    switch ($randNum) {
      case 10: $randNum = &#39;A&#39;; break;
      case 11: $randNum = &#39;B&#39;; break;
      case 12: $randNum = &#39;C&#39;; break;
      case 13: $randNum = &#39;D&#39;; break;
      case 14: $randNum = &#39;E&#39;; break;
      case 15: $randNum = &#39;F&#39;; break;
    }
    $str .= $randNum;
  }
  return $str;
}
$color = randomColor();
echo $color;
?>

Running results: #8ABED4

##Related recommendations:

PHP function conversion of decimal, binary, octal and hexadecimal

Js string conversion methodHexadecimal

php function hex2bin() that converts a string of hexadecimal values ​​into ASCII characters

The above is the detailed content of How to implement hexadecimal color random generator function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn