PHP String str_shuffle() Function
The PHP String str_shuffle() function is used to randomly shuffle each character in a string that is passed as an input to the function. When a number is given in, it shuffles it like a string. This function does not change the original string or the number passed in as an argument. Rather, it returns a new string that is one of the several permutations of the string that was passed to it in the parameter.
Caution: This function should not be used for cryptographic or applications that need unguessable returned values since it does not produce cryptographically secure values.
Syntax
Below is the syntax of the PHP String str_shuffle() function −
string str_shuffle ( string $string )
Parameters
This function accepts $string parameter which is a required parameter and it is used to specify the string to shuffle.
Return Value
The str_shuffle() function returns a string with jumbled characters that is the same length. The output varies every time the program is run since the characters are scrambled differently. The original string or the number could be the return value in some circumstances.
PHP Version
First introduced in core PHP 4.3.0, the str_shuffle() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
Here is the basic example of the PHP String str_shuffle() function to simply take a string and shuffle its characters.
<?php // Basic example $string = "abcdef"; $shuffled = str_shuffle($string); echo "Original: $string\n"; echo "Shuffled: $shuffled\n"; ?>
Output
Here is the outcome of the following code −
Original: abcdef Shuffled: edcabf
Example 2
In the below PHP code we will use the str_shuffle() function and generate different results each time it is called.
<?php
// Shuffle the same string multiple times
$string = "hello";
echo "Original: $string\n";
for ($i = 1; $i <= 3; $i++) {
$shuffled = str_shuffle($string);
echo "Shuffled $i: $shuffled\n";
}
?>
Output
This will generate the below output −
Original: hello Shuffled 1: loleh Shuffled 2: ehllo Shuffled 3: leohl
Example 3
To generate a random password, this program uses the str_shuffle() function to shuffle a preset string of characters.
<?php // Generate a random password $characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"; // Shuffle and take the first 8 characters $password = substr(str_shuffle($characters), 0, 8); echo "Generated Password: $password\n"; ?>
Output
This will create the below output −
Generated Password: kF%QMsrn
Example 4
In the following example, we are using the str_shuffle() function to shuffle the words in the given sentence rather than individual characters.
<?php
// Define a string
$string = "PHP is a popular scripting language";
// Split string into words
$words = explode(" ", $string);
// Shuffle characters of combined words
$shuffledWords = str_shuffle(implode("", $words));
$shuffledString = str_split($shuffledWords);
echo "Original Sentence: $string\n";
echo "Shuffled Words: $shuffledWords\n";
?>
Output
Following is the output of the above code −
Original Sentence: PHP is a popular scripting language Shuffled Words: lgpranioanHPsiaesupPiuglgctrpa