PHP str_shuffle()

cslylla's avatar
Published May 22, 2023Updated Jul 28, 2023

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

  • 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

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:

<?php
echo 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.

Code
Output

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

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