PHP str_shuffle()
The str_shuffle() function randomly shuffles the characters of a string. It generates a different order of characters each time it is called.
Syntax
str_shuffle($string)
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.
Example
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:
<?phpecho str_shuffle("secret");?>
The example can result in the following output (it can be any combination of the given letters):
tesecr
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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours