String, Number and Array Shuffling Techniques in PHP

Shuffling String Number and Array in PHP

Hello friends, welcome to the todays post on PHP. Today we are going to learn about how to shuffle a string, a number and an array in PHP. Shuffling is most frequently used option in a trivia game,  examination management system or any kind of puzzle game. Hopefully the following tutorial will be helpful for your in your project.

Lets get started-

1. Shuffle string:

For randomizing or shuffling characters in a word or a full sentence we can use str_shuffle function. str_shuffle function takes a string as input and produce random string each time as output. You can also called it randomization function for the string.

string str_shuffle ( string str )

– Example of str_shuffle function:

$str = '';

$shuffled = str_shuffle($str); // This will echo something like: nca.tajaxlooe

echo $shuffled;

2. Shuffle number:

rand() function uses to generate random integer numbers. It takes two parameter as input and produce a single random number. First parameter for starting number of range and second parameter use for ending point of range. If you leave both parameters blank, then rand() function generates an arbitrary random number.

int rand($int min, $int max)

– Example of rand() function:

// Example: 1

$random_number = rand(100,200);

echo $random_number; // Output: 155

// Example: 2

$random_number = rand();

echo $random_number; // Possible Output: 4532

3. Shuffle Array:

Shuffle() function uses for shuffling the array items. This function takes an array as input and produced a new array with new index as output.

bool shuffle($array)

– Example of shuffle() function:

$my_friends = array('0'=>"MAK", '1'=>"MIK", '2'=>"Xenious");


$shuffle_array = shuffle($my_friends);


Was this information useful? What other tips would you like to read about in the future? Please share your comments, feedback and experiences with us by commenting below!

Total 0 Votes

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Comment

Back To Top