Content · Programming · PHP

PHP str_shuffle() Function: Usage and Examples

Learn str_shuffle in PHP with clear syntax, three code examples, important edge cases, common beginner mistakes, and related APIs.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When you need to shuffle the bytes in a string, it helps to understand both the normal result and what changes with different inputs. This guide focuses on practical examples rather than repeating PHP background material.

str_shuffle provides the behavior described below. The order is random and output varies from run to run. The input string remains unchanged.

What Is str_shuffle in PHP?

Shuffle the bytes in a string. The order is random and output varies from run to run. The input string remains unchanged.

Its syntax is:

str_shuffle(string $string): string

Parameters and return value: Read the argument names and optional values in the signature above. Methods use an existing object, while stand-alone functions take direct arguments. The examples demonstrate the relevant arguments and the value returned or the object modified.

Important: Results are nondeterministic. Do not use byte shuffling for Unicode graphemes or security tokens.

Example 1: Basic Usage

This first example demonstrates how to shuffle the bytes in a string with a small input.

<?php

$letters = "ABCDE";
echo str_shuffle($letters);

Output:

EBADC

The example uses the simplest useful form. The order is random and output varies from run to run. The input string remains unchanged. The output may depend on the active ICU locale, PHP configuration, or input supplied to the function.

Example 2: Another Case

Here the input or option changes, making it easier to see where the behavior differs. Results are nondeterministic. Do not use byte shuffling for Unicode graphemes or security tokens.

<?php

echo strlen(str_shuffle("123456"));

Output:

6

Compare the result to the first example. Random\Randomizer supports explicit engines; random_bytes() is appropriate for cryptographic randomness.

Practical Example: Applying the Function

This standalone example shows how the same operation fits into a small application task.

<?php

$alphabet = "ABCDEFGH";
$shuffledAlphabet = str_shuffle($alphabet);
echo "Original: " . $alphabet . PHP_EOL;
echo "Shuffled: " . $shuffledAlphabet;

Output:

Original: ABCDEFGH
Shuffled: BFDHEAGC

The example keeps the operation visible and gives you a starting point for using it in an application. Results are nondeterministic. Do not use byte shuffling for Unicode graphemes or security tokens.

Important Behavior and Edge Cases

  • Core behavior: The order is random and output varies from run to run. The input string remains unchanged.
  • Return value: Check the result separately from changes to passed-in arrays, iterators, or object state.
  • Data constraints: Results are nondeterministic. Do not use byte shuffling for Unicode graphemes or security tokens.
  • Environment: Run this example under a PHP version supporting the API; check results and exceptions before using them.

Comparison With Related Functions

Function or ApproachWhen to Use It
str_shuffleShuffle the bytes in a string.
Related PHP APIsRandom\Randomizer supports explicit engines; random_bytes() is appropriate for cryptographic randomness.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming the function works on every kind of inputFollow the declared argument types and check the input format before calling it.
Ignoring the behavior of the resultResults are nondeterministic. Do not use byte shuffling for Unicode graphemes or security tokens.
Using the wrong related PHP APIRandom\Randomizer supports explicit engines; random_bytes() is appropriate for cryptographic randomness.

Key Takeaways

  • str_shuffle helps you shuffle the bytes in a string.
  • The order is random and output varies from run to run. The input string remains unchanged.
  • Results are nondeterministic. Do not use byte shuffling for Unicode graphemes or security tokens.
  • Random\Randomizer supports explicit engines; random_bytes() is appropriate for cryptographic randomness.

str_shuffle is useful when you need to shuffle the bytes in a string. Start with the small examples, then adapt the practical example while respecting its input and environment requirements.

Share this resourceSend this page to someone who may find it useful.