Random String Generator Algorithm in PHP.

Omo Junior
5 min readOct 22, 2022

--

The algorithm in this article can be used in real-world scenarios where you would like to generate a specific length of string for a user or authentication purposes.

By Author

This Article will be brief it won’t take you more than 3 minutes, but there are important things to learn here.

What is an algorithm?

An algorithm is a procedure used for solving a problem. Algorithms act as an exact list of instructions that conduct specified actions sequentially.

Let's get to the topic...

By Author

The function alone should give us a clear understanding of what to expect from our code. for our function to execute the block of codes within it, it expects two arguments, and they are assigned a default value of null, well we can improve that but since our goal in this article is to understand the algorithm we will stick to the null values.

Why is the result variable set to false?

By Author

You probably wondering why I am using false as the default value for the result variable, well I most often set the variable that will output the results of the algorithm to false, which I assume makes the computer understand that we haven't provided an input that the output variable will be assigned to. Just my personal preference.

Let's get to the main reason why you are here.

First, we know that our length variable default value is null and here we are setting the input value to be greater than 8, a bit confusing but as I said earlier on we can improve this algorithm.

The main function of the length variable is to take a value that will be outputted as the length of our generated string, in our condition we are simply telling our code that if any input that is going to be assigned to the length variable should be greater than 8 and if our input value does not satisfy our condition we will echo a message and exit the code block.

What about the other conditions?

Now we are going deep, but not that deep yet, if you rewind, our type variable is assigned null as its default value but here in our condition we are expecting it to equal four values and if it does we will generate the string type based on any of the four values assigned to our type variable.

oops! hold on don’t you think we left out something?

hmm yeah, I did but like I said we can improve the code, let me know what should have been added as a comment.

let's move on, in our condition if the input value matches any of the four values our type condition expects, we will assign generated string type to a new variable called charactersType which simply means the characters that will be randomly generated in the output result.

Now we have a set of rules that our code block will sequentially follow to give us a result, let’s move to the second part.

Before we get into the final part of our algorithm, I would like you to know and understand two PHP built-in functions that I am using here.

1 str_split() — Convert a string to an array.
2 array_rand() — Pick one or more random keys out of an array.
//for better understanding you can check up the functions on php.net

Remember we have assigned the string type values to the $charactersType variable, and here we are using the built-in function (str_split) to convert our $charactersType to an array that is being assigned to a new variable called charactersArray.
we are going to run a loop and in our loop, we have set the $i variable to 0 like in any other for loop, and our condition is checking if our $i variable is lesser than the value of our length variable we will keep the loop going till it meet our condition.

Within the loop code block, we are making use of the built-in function(array_rand) which will pick random keys from our $charactersArray and assign the random values to the $randCharArray variable. randCharArray is the abbreviation of randomCharacterArray.

Now it is time for us to generate our results.

By Author

If we rewind to when we started our code, I set the value of $result to false, and in our case here, we are assigning a new value to our result variable, but instead of using result = the new value, we concatenated the $charactersArray variable with the $randCharArray variable as the key.

That should now give us the results we expect from our algorithm.

Conclusion

There's a lot that we can do to improve this algorithm since our main purpose here is to understand the sequence and steps we took in getting to the result of our algorithm I will conclude by leaving some possible things that we should have added as an option for us to talk about in the comment.

Thanks for reading and I look forward to your suggestions.

--

--

Omo Junior

Full-Stack Developer, Co.Founder @Omomakay Liberia, Founder @Beyla AI