Random numbers and secure tokens

Use random_int() and random_bytes() for anything security related. rand() and mt_rand() are not safe for that.

PHP · Random numbers and secure tokensBeginner
$dice  = random_int(1, 6);                    // secure random number
$token = bin2hex(random_bytes(16));           // 32 hex characters
$code  = str_pad((string)random_int(0, 999999), 6, '0', STR_PAD_LEFT);  // 6-digit code

$items = ['a', 'b', 'c'];
$pick  = $items[array_rand($items)];          // one random item
shuffle($items);                              // random order
randomrandom_inttoken