All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Generate Random String Fixed Length

Last Updated : Mar 11, 2024

PHP Generate Random String Fixed Length

In this tutorial we will show you the solution of PHP generate random string fixed length, mainly there are many ways with help of which we are able to generate random strings.

In previous tutorials, we done that how we can generate a random number of fixed length whereas in this article we are going to done same task but using string here.

Step By Step Guide On PHP Generate Random String Fixed Length :-

Here, we already said that there are many ways. So, let us understand with most efficient way that is by creating a function and using for loop.

Here below we are going to show you code in which we understand this method properly.

<!DOCTYPE html>
      <html>
        <head>
          <title> php generate random string fixed length </title>
   </head>
 <body>
 <?php
$n=8;
function randomNumber($n) {
 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
 $randomString = '';
 for ($i = 0; $i < $n; $i++) {
  $index = rand(0, strlen($characters) - 1);
  $randomString .= $characters[$index];
 }
 return $randomString;
}
echo randomNumber($n);
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as we can see that we create our basic php structure. And inside this structure as we see that we give a limit. This is because we want to show string of fixed length. And we generate here string of 8.
  6. After that we create a function and pass a parameter which we call later. Inside this function we store our all characters in $characters variable. In this case all characters are characters which we want to show.
  7. Now, we use for loop to generate random string and inside this rand function is used. As we know rand is used to generate a random number. We use this rand on our specified characters and after that we store them in a variable that is randomString.
  8. At last we return this randomString and call function at end of function. you can see reference in above code.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how we are able to generate random string of fixed length using php.

I hope this tutorial on PHP generate random string fixed length helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪