본문 바로가기

WEB/PHP

PHP uniqid 고유값 유니크값 난수값 생성하기


고유값 유니크값 생성하기

참조

https://zetawiki.com/wiki/PHP_uniqid()

 

PHP uniqid()

- 유니크 ID를 생성하는 PHP 함수[1]
- 기본은 16진수 13자리
- 앞 8자리는 초단위까지, 뒤 5자리는 마이크로초단위를 16진수로 바꾼 것

예제1)

echo strtoupper(uniqid());

// 대문자(strtoupper)로 유니크값(uniqid) 생성
// 출력값 : 5DF86FB174DF1

 

예제2)

//특정 규칙으로 정의

function uuidgen() {

  return sprintf('%04x-%04x-%04x-%04x-%04x-%04x',

      mt_rand(0, 0xffff),

      mt_rand(0, 0xffff),

      mt_rand(0, 0xffff),

      mt_rand(0, 0xffff),

      mt_rand(0, 0xffff),

      mt_rand(0, 0xffff)

  );

}

echo strtoupper(uuidgen());

// 출력값 : 1DE1-8AAB-79B5-190D-45B4-2677

반응형

'WEB > PHP' 카테고리의 다른 글

php 텍스트 별표 바꾸기  (0) 2019.10.07
PHP 배열 이중 반복문 예제  (0) 2019.03.19