Published May 22, 2023Updated Jul 28, 2023
Contribute to Docs

The str_shuffle() function randomly shuffles the characters of a string. It generates a different order of characters each time it is called.



The str_shuffle() function takes one required parameter: a $string that is a string to be shuffled. It returns a string with the shuffled characters.


The following example uses the str_shuffle() function to shuffle the letters of the word "secret". Then the echo command prints the shuffled letters as a string to the console:

echo str_shuffle("secret");

The example can result in the following output (it can be any combination of the given letters):


Codebyte Example

This example uses the str_shuffle() function with a string.


Note: The output will vary each time this program is executed, as the str_shuffle() function shuffles the string randomly. The function should not be used for cryptography because it does not generate cryptographically secure random numbers.

All contributors

Looking to contribute?

Learn PHP on Codecademy