How to generate a random number
Short answer
random_int() returns a random integer; both bounds are inclusive:
<?php
echo random_int(1, 100);A random array element
array_rand() returns a random key of the array:
<?php
$colors = ['red', 'green', 'blue'];
echo $colors[array_rand($colors)];Shuffling an array
shuffle() reorders the elements of the array itself at random:
<?php
$cards = [1, 2, 3, 4, 5];
shuffle($cards);
echo implode(', ', $cards);Functions used in this recipe
- random_int — Returns a random integer from a range.