PHP str_shuffle() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, str_shuffle() randomly rearranges the characters of a string.

The result has the same characters in a random order. It returns a different result each time you run it.

Syntax

Call str_shuffle() with the values you need. Start with this line.

PHP
$mixed = str_shuffle("abcdef");
CallWhat it does
str_shuffle("abc")Returns something like "bca" or "cab" (random).
strlen(str_shuffle("PHP123"))Always returns 6 because the length does not change.
substr(str_shuffle($pool), 0, 6)Picks 6 random characters from a pool.

str_shuffle() uses a non-secure random generator. Do not use it for passwords, tokens, or anything security related. Use random_int() or random_bytes() for that.

Library declarations
PHP
str_shuffle(string $string): string

Parameters

ParameterDescription
stringRequired. The string to shuffle.

Return Value

Return ValueDescription
stringThe shuffled string with the same length and characters.

PHP Examples

Example 1 — Shuffle a String

The characters change order, but the length stays the same.

PHP
<?php
$shuffled = str_shuffle("PHP123");

echo strlen($shuffled), PHP_EOL;

How It Works

  1. Pass a six-character string to str_shuffle.
  2. PHP rearranges the characters randomly.
  3. The exact order changes on every run.
  4. The length is always 6, so strlen prints 6.

Example 2 — Same Characters, New Order

Sort the shuffled result to prove no character was lost.

PHP
<?php
$shuffled = str_shuffle("PHP123");
$chars = str_split($shuffled);

sort($chars);

echo implode("", $chars), PHP_EOL;

How It Works

  1. Shuffle the string. The order is random.
  2. Split the result into an array of characters.
  3. Sort the array. The random order disappears.
  4. The sorted result is always the same: 123HPP.

Example 3 — Pick a Random Code

Shuffle a pool of characters and keep the first few.

PHP
<?php
$pool = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
$code = substr(str_shuffle($pool), 0, 6);

echo strlen($code), PHP_EOL;

How It Works

  1. Create a pool of letters and numbers.
  2. Shuffle the pool so the characters are in random order.
  3. Take the first 6 characters with substr.
  4. The code differs each run, but its length is always 6.

Frequently Asked Questions

str_shuffle() randomly reorders the characters of a string and returns the new string.
No. It is not suitable for passwords or tokens. Use random_int() or random_bytes() for security.
No. The shuffled string has the same characters and length as the original.
Shuffle the pool and take the first few characters with substr(str_shuffle($pool), 0, $n). Note that characters will not repeat.
You can seed the generator with mt_srand() to get a repeatable order, but do this only for testing, not for security.

Next: str_split()

str_split() splits a string into an array of smaller strings of a fixed length.

str_split() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful