Skip to content

Instantly share code, notes, and snippets.

@Yohn
Created January 9, 2023 05:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yohn/db5b82445d1066cb25ecbf9ba3f47e34 to your computer and use it in GitHub Desktop.
Save Yohn/db5b82445d1066cb25ecbf9ba3f47e34 to your computer and use it in GitHub Desktop.
Simple generate random string function in PHP with max length option and easy to change characters.
<?php
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment