The str_shuffle Function

The str_shuffle function shuffles the characters in a string in random order. Works correctly only with Latin characters (single-byte characters).

Syntax

phpstr_shuffle(string $string): string

Example

Let's shuffle the characters of a string in random order:

php<?php
  echo str_shuffle('abcde');
?>

Example

Let's get a string of 3 digits so that the digits do not repeat:

php<?php
  $digits = '0123456789';
  $shuffled = str_shuffle($digits);
  $res = substr($shuffled, 0, 3);
  
  echo $res;
?>

See Also

  • the shuffle function,
    which shuffles the elements of an array in random order
  • the array_rand function,
    which selects random values from an array
  • the mt_rand function,
    which generates random numbers