How to generate random string in PHP

Must Read : How to generate random string in PHP

How to generate random string in PHP

Have you ever needed to generate a random password from a given set of chars in PHP? You could also probably wanted a password salt used for encrypting the user password. Well here are two solutions which I can think of at the moment to help you generate the random string in php.

1st solution

The first solution is by using the str_shuffle() function, the same as shuffle() which I’ve showed you how to suffle an array when choosing a random background image, you would use this function on a specific string to shuffle its characters..

(PHP 4 >= 4.3.0, PHP 5)

You can of course use the function directly without creating your own function for this, but for the sake of this being more easily used in different cases, this how you do it.

Output

The output would look similar to:

There are probably other ways to do this, but for now, these solutions should suffice.

Anyway, thank you for reading this and I hope you found it interesting.

(Visited 46 times, 1 visits today)

Leave a Comment.