Oihana PHP

randomKey.php

Table of Contents

Functions

randomKey()  : string
Generates a random key string, optionally prefixed with a given string and separated by a custom separator.

Functions

randomKey()

Generates a random key string, optionally prefixed with a given string and separated by a custom separator.

randomKey(string|null $prefix[, string $separator = '_' ]) : string

The random part is generated using mt_rand() which provides a pseudo-random integer.

Parameters
$prefix : string|null

Optional prefix to prepend to the key. If null or empty, no prefix is added.

$separator : string = '_'

Separator string between the prefix and the random number. Defaults to underscore '_'.

Tags
example
echo randomKey("user");       // Possible output: "user_123456789"
echo randomKey(null);         // Possible output: "987654321"
echo randomKey("order", "-"); // Possible output: "order-456789123"
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The generated random key.


        
On this page

Search results